diff --git a/TASKS/README.md b/TASKS/README.md index d541f8d..4b765fe 100644 --- a/TASKS/README.md +++ b/TASKS/README.md @@ -1 +1,2 @@ this directory contains tasks on each of the lessons of c programming, attempt each of them before asking or checking the solutions. + diff --git a/TASKS/Task 001.c b/TASKS/Task 001.c index 24be041..43c737f 100644 --- a/TASKS/Task 001.c +++ b/TASKS/Task 001.c @@ -1,7 +1,22 @@ -/* Write your program after the comment -Write a program that prints the alphabet in lowercase, followed by a new line. +#include +#include -Print all the letters except q and e +/** + * Write your program after the comment + * Write a program that prints the alphabet in lowercase, followed by a new line. + * Print all the letters except q and e + * use printf + */ -use printf -*/ \ No newline at end of file +int main(void) +{ + char alpha; + + for (alpha = 'a'; alpha <= 'z'; alpha++) + { + if (alpha == 'e' || alpha == 'q') + continue; + printf("%c", alpha); + } + printf("\n"); +} diff --git a/TASKS/Task 002.c b/TASKS/Task 002.c index 5cdd390..c13f0b3 100644 --- a/TASKS/Task 002.c +++ b/TASKS/Task 002.c @@ -1,6 +1,26 @@ -/* Write your program after the comment -Write a program that asks and reads the score of a user -If the score is less than 80, display to the user that they -are not elgible to be enrolled. -If the score is greater than or equal 80, they can be enrolled. -*/ \ No newline at end of file +#include +#include +#include + +/** + * Write your program after the comment + * Write a program that asks and reads the score of a user + * If the score is less than 80, display to the user that they are not eligible to be enrolled. + * If the score is greater than or equal 80, they can be enrolled. + */ + +int main(void) +{ + int score = 0; + printf("Enter a Score:\t"); + if (scanf("%d", &score) == 1) + { + if (score < 80) + printf("You are not eligible to be enrolled."); + else if (score >= 80) + printf("You can be enrolled."); + } + else + printf("Your input is not a numeric data."); + return 0; +} diff --git a/TASKS/Task 003.c b/TASKS/Task 003.c index 7fca79c..0a88856 100644 --- a/TASKS/Task 003.c +++ b/TASKS/Task 003.c @@ -1,9 +1,23 @@ -/* Write your program after the comment -Write a program that reads input from user (Number) -If the number is Odd, display 'userInput is Odd' -If the number is even, display 'userInput is even' -If the number is zero, display 'This is zero' +#include +#include -NOTE: 'userInput' should be the number entered by the user. +/** + * Write your program after the comment + * Write a program that reads input from user (Number) + * If the number is Odd, display 'userInput is Odd' + * If the number is even, display 'userInput is even' + * If the number is zero, display 'This is zero' + * + * NOTE: 'userInput' should be the number entered by the user. + */ -*/ \ No newline at end of file +int main(void) +{ + int num = 0; + + printf("Enter Number:\t"); + scanf("%d", &num); + + (num == 0) ? printf("Input is Zero") : ((num % 2 == 0) ? printf("Input is even") : printf("input is odd")); + return (0); +} diff --git a/TASKS/Task 004.c b/TASKS/Task 004.c index 17ee38c..3479781 100644 --- a/TASKS/Task 004.c +++ b/TASKS/Task 004.c @@ -1,14 +1,34 @@ -/* Write your program after the comment -Write a program that asks and reads the following input from a use; -Your name -Your age -and then displays; Your name is 'name' and you are 'age' years old. +#include +#include -Example; -what is your name: David -How old are you?: 65 +/** + * Write your program after the comment + * Write a program that asks and reads the following input from a use; + * Your name + * Your age + * and then displays; Your name is 'name' and you are 'age' years old. + * + * Example; + * + * what is your name: David + * How old are you?: 65 + * + * Output: + * Your name is David and you are 65 years old. + */ -Output: -Your name is David and you are 65 years old. +int main(void) +{ + int age = 0; + char name[100]; -*/ \ No newline at end of file + printf("Enter Name:\t"); + scanf("%s", name); + + printf("Enter Age:\t"); + scanf("%d", &age); + + printf("Your name is %s and you are %d years old.", name, age); + + return (0); +} diff --git a/TASKS/Task 005.c b/TASKS/Task 005.c index db32ab3..08d6b8a 100644 --- a/TASKS/Task 005.c +++ b/TASKS/Task 005.c @@ -1,4 +1,27 @@ -/* - Write a program in C to find the square of any number -using the function. -*/ \ No newline at end of file +#include + +/** + * Write a program in C to find the square of any number + * using the function. + */ + + void return_square() + { + int num = 0, result; + + printf("Enter a Number:\t"); + if (scanf("%d", &num) == 1) + result = (num * num); + else + result = 0; + if (result == 0) + printf("\nThe data entered is not a number. The result is %d", result); + else + printf("\nThe square of %d = %d", num, result); + } + + int main(void) + { + return_square(); + return (0); + } \ No newline at end of file diff --git a/TASKS/Task 006.c b/TASKS/Task 006.c index dc0b9af..af09de4 100644 --- a/TASKS/Task 006.c +++ b/TASKS/Task 006.c @@ -1,4 +1,26 @@ +#include /* Write a program in C to check a given number is even or odd using the function. -*/ \ No newline at end of file +*/ +void type_checker() +{ + int num = 0; + + printf("Enter a Number:\t"); + if (scanf("%d", &num) == 1) + { + if ((num % 2) == 0) + printf("\nThe number %d is an even number.", num); + else + printf("\nThe number %d is an odd number.", num); + } + else + printf("\nThe data entered is not a number."); + +} +int main(void) +{ + type_checker(); + return (0); +} \ No newline at end of file diff --git a/TASKS/Task 007.c b/TASKS/Task 007.c index 07ea0fb..2f09563 100644 --- a/TASKS/Task 007.c +++ b/TASKS/Task 007.c @@ -1,5 +1,38 @@ +#include +#include + /* write your solution after the comment Write a function that prints the alphabet, in uppercase, followed by a new line. -*/ \ No newline at end of file +*/ + +void to_upper_case_one() +{ + char n; + + for (n = 'A'; n <= 'Z'; n++) + { + printf("%c", n); + } + printf("\n"); +} + +void to_upper_case_two() +{ + char n; + + for (n = 'a'; n <= 'z'; n++) + { + printf("%c", toupper(n)); + } + printf("\n"); +} + +int main(void) +{ + to_upper_case_one(); + to_upper_case_two(); + + return (0); +} \ No newline at end of file diff --git a/TASKS/Task 008.c b/TASKS/Task 008.c index 0a815b2..2a4aed2 100644 --- a/TASKS/Task 008.c +++ b/TASKS/Task 008.c @@ -1,4 +1,37 @@ +#include +#include + /*write your solution after the comment Write a function that prints the alphabet, in lowercase followed by a new line. -*/ \ No newline at end of file +*/ + +void to_lower_case_one() +{ + char n; + + for (n = 'a'; n <= 'z'; n++) + { + printf("%c", n); + } + printf("\n"); +} + +void to_lower_case_two() +{ + char n; + + for (n = 'A'; n <= 'Z'; n++) + { + printf("%c", tolower(n)); + } + printf("\n"); +} + +int main(void) +{ + to_lower_case_one(); + to_lower_case_two(); + + return (0); +} \ No newline at end of file diff --git a/TASKS/Task 009.c b/TASKS/Task 009.c index d481f90..ca7b43c 100644 --- a/TASKS/Task 009.c +++ b/TASKS/Task 009.c @@ -1,5 +1,28 @@ +#include +#include +#include + /*write your solution after the comment Write a function that checks for lowercase character. That is, when passed in an arguments, it checks if the argument is lowercase or uppercase. -*/ \ No newline at end of file +*/ +void check_case(char chr) +{ + if ( chr >= 'a' && chr <= 'z') + printf("\n%c is a lower cased character.", chr); + else if (chr >= 'A' && chr <= 'Z') + printf("\n%c is an upper cased character.", chr); + else + printf("\nKindly provide a character."); +} +int main(void) +{ + char str; + + printf("Enter a character:\t"); + scanf("%c", &str); + check_case(str); + + return (0); +} \ No newline at end of file diff --git a/TASKS/Task 010.c b/TASKS/Task 010.c index 62c07e9..1243610 100644 --- a/TASKS/Task 010.c +++ b/TASKS/Task 010.c @@ -1,5 +1,30 @@ +#include + /*write your solution after the comment Write a function that computes the absolute value of an integer. Understand what an absolute value is, before attempting it. -*/ \ No newline at end of file +*/ +int get_absolute(int num) +{ + if (num < 0) + num *= -1; + return (num); +} + +int main(void) +{ + int num = 0; + int result = 0; + + printf("Enter a Number:\t"); + if (scanf("%d", &num) == 1) + { + result = get_absolute(num); + printf("The absolute value of %d = %d", num, result); + } + else + printf("\nThe data entered is not a number."); + + return (0); +} \ No newline at end of file diff --git a/TASKS/Task 011.c b/TASKS/Task 011.c index 96c4add..f8698ad 100644 --- a/TASKS/Task 011.c +++ b/TASKS/Task 011.c @@ -1,3 +1,5 @@ +#include + /*write your solution after the comment Write a function that prints the 9 times table, starting with 0. @@ -6,4 +8,18 @@ Output should look like this; 9 x 1 = 9 9 x 2 = 18 ... and so on till 12 -*/ \ No newline at end of file +*/ + +void times_table() +{ + int i = 9, j; + + for (j = 0; j <= 12; j++) + printf("%d * %d = %d\n", i, j, (i * j)); + printf("\n"); +} +int main(void) +{ + times_table(); + return (0); +} \ No newline at end of file diff --git a/TASKS/Task 012.c b/TASKS/Task 012.c index 735a015..a99e386 100644 --- a/TASKS/Task 012.c +++ b/TASKS/Task 012.c @@ -1,4 +1,36 @@ +#include +#include /*Write your solution after the comment Write a function that adds two integers and prints the result. -*/ \ No newline at end of file +*/ + +int accept_value(char *chr) +{ + int var = 0; + + printf("Enter %s Value:\t", chr); + if( scanf("%d", &var) == 1) + return (var); + else + return (0); +} + +void add_up() +{ + int sum = 0; + int first; + int second; + + first = accept_value("First"); + second = accept_value("Second"); + sum = (first + second); + printf("\nThe sum of %d + %d = %d.\n", first, second, sum); +} + +int main(void) +{ + add_up(); + + return (0); +} \ No newline at end of file diff --git a/TASKS/Task 013.c b/TASKS/Task 013.c index 068a6cc..4316cc4 100644 --- a/TASKS/Task 013.c +++ b/TASKS/Task 013.c @@ -1,6 +1,32 @@ +#include + /*Write your solution after the comment Write a function that prints all natural numbers from n to 100, followed by a new line. That is n is your argument, it should print from any number passed as argument till 100. -*/ \ No newline at end of file +*/ + +void natural_num_print(int arg) +{ + int count = 100; + + while (arg <= count) + { + printf("%d ", arg); + arg++; + } + printf("\n"); +} +int main(void) +{ + int num = 0; + + printf("Enter A Value:\t"); + if (scanf("%d", &num) == 1) + natural_num_print(num); + else + printf("\nPlease enter a number."); + + return (0); +} \ No newline at end of file diff --git a/TASKS/Task 014.c b/TASKS/Task 014.c index d139cd2..64ac4ed 100644 --- a/TASKS/Task 014.c +++ b/TASKS/Task 014.c @@ -1,3 +1,5 @@ +#include + /*Write your solution after the comment Write a function that prints 10 times the numbers, from 0 to 14, followed by a new line. @@ -6,4 +8,27 @@ Your output should look like this; 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...up to 10 times -*/ \ No newline at end of file +*/ + +void print_14() +{ + int i; + + for (i = 0; i <= 14; i++) + printf("%d ", i); + printf("\n"); +} +void print_10() +{ + int i; + + for (i = 0; i < 10; i++) + print_14(); + printf("\n"); +} + +int main(void) +{ + print_10(); + return (0); +} \ No newline at end of file diff --git a/TASKS/Task 015.c b/TASKS/Task 015.c index 7cac93a..ce343ee 100644 --- a/TASKS/Task 015.c +++ b/TASKS/Task 015.c @@ -1,3 +1,5 @@ +#include + /*Write your solution after the comment Fizz-Buzz - Write a program that prints the numbers from 1 to 100, followed by a new line. But for multiples @@ -6,4 +8,24 @@ multiples of five print Buzz. For numbers which are multiples of both three and five print FizzBuzz. Your output should like this; 1 2 fizz 4 buzz fizz 7 8 fizz... and so on. -*/ \ No newline at end of file +*/ + +void fizzbuzz_game(){ + int i = 0; + + for (i = 1; i <= 100; i++){ + if (i % 3 == 0 && i % 5 == 0) + printf("FizzBuzz\t"); + else if (i % 5 == 0) + printf("Buzz\t"); + else if (i % 3 == 0) + printf("Fizz\t"); + else + printf("%d\t", i); + } +} +int main(void) +{ + fizzbuzz_game(); + return (0); +} \ No newline at end of file diff --git a/TASKS/Task 016.c b/TASKS/Task 016.c index 8700bc0..0a608aa 100644 --- a/TASKS/Task 016.c +++ b/TASKS/Task 016.c @@ -1,3 +1,5 @@ +#include + /*Write your solution after the comment Write a recursive function that takes an integer as an argument and returns the product of all integers @@ -8,4 +10,28 @@ Output: 720 // That is 1x2x3x4x5x6 = 720 -*/ \ No newline at end of file +*/ + +int mult(int); + +int main() { + int num; + + num = 6; + printf("%d\n", mult(num)); + return 0; +} + +/** + * mult - function to recursively multiply a number till it is 1 + * @num - number to multiply till it is 1 + * Return: the recursive product + */ +int mult(int num) +{ + if (num == 1) + return (num); + if (num < 1) + return (0); + return ( num * mult(num -1)); +} diff --git a/TASKS/Task 017.c b/TASKS/Task 017.c index f5ef778..a050d6a 100644 --- a/TASKS/Task 017.c +++ b/TASKS/Task 017.c @@ -1,3 +1,5 @@ +#include + /* Write a recursive function that takes an integer as an argument and returns the sum of all integers from @@ -8,4 +10,28 @@ Output: 21 // That is; 1+2+3+4+5+6 = 21 -*/ \ No newline at end of file +*/ + +int add(int); + +int main() { + int num; + + num = 6; + printf("%d\n", add(num)); + return 0; +} + +/** + * add - function to recursively add a number till it is 1 + * @num - number to add till it is 1 + * Return: the recursive sum + */ +int add(int num) +{ + if (num == 1) + return (num); + if (num < 1) + return (0); + return ( num + add(num -1)); +} diff --git a/TASKS/Task 018.c b/TASKS/Task 018.c index 147ff9a..76a0535 100644 --- a/TASKS/Task 018.c +++ b/TASKS/Task 018.c @@ -1,5 +1,30 @@ +#include + /* Write a function that returns the value of x raised to the power of y. functionName(int x, int y); -*/ \ No newline at end of file +*/ + +int get_power(int x, int y) +{ + if (y < 0) + return ((double)(1.0/(x * get_power(x, (-1) * (y + 1))))); + else if (y == 0) + return (1); + else if (y == 1) + return (x); + else + return (x * get_power(x, (y - 1))); +} + +int main() { + int x, y; + + x = y = 0; + printf("Enter 2 Numbers separated by a space:\t"); + scanf("%d %d", &x, &y); + + printf("%d\n", get_power(x, y)); + return 0; +} diff --git a/TASKS/Task 019.c b/TASKS/Task 019.c index 5cc5874..dbb85f2 100644 --- a/TASKS/Task 019.c +++ b/TASKS/Task 019.c @@ -1,4 +1,55 @@ +#include + /* Write a function to convert temperature from Celsius to Fahrenheit and vice versa. -*/ \ No newline at end of file +*/ + +/** + * farenheit_to_celius - convert Fahrenheit temperature to Celcius + * @temp: temperature value in celcius (double) + * Return: The Fahrenheit value after computation/conversion + */ + +double farenheit_to_celius(double temp) +{ + double result; + + result = ((temp - 32) * 5) / 9; + return (result); +} + +/** + * farenheit_to_celius - convert Celcius temperature to Fahrenheit + * @temp: temperature value in fahrenheit (double) + * Return: The Celcius value after computation/conversion + */ + +double celius_to_farenheit(double temp) +{ + double result; + + result = (temp * 9 / 5) + 32; + return (result); +} + +int main(void) +{ + double num = 0; + double fah_val; + double cel_val; + + printf("Enter a Temperature in Celcius (*C):\t"); + if (scanf("%lf", &num) == 1) + { + fah_val = celius_to_farenheit(num); + printf("\nConverting %.2f celcius to fahrenheit = %.2f\n", num, fah_val); + + cel_val = farenheit_to_celius(fah_val); + printf("\nConverting %.2f fahrenheit to celcius = %.2f\n", fah_val, cel_val); + return (0); + } + printf("\nYou did not enter a numeric data.\n"); + return (1); + +} \ No newline at end of file diff --git a/TASKS/Task-21-Pointers-Quiz.md b/TASKS/Task-21-Pointers-Quiz.md index 9f783c2..0599a19 100644 --- a/TASKS/Task-21-Pointers-Quiz.md +++ b/TASKS/Task-21-Pointers-Quiz.md @@ -1,17 +1,88 @@ -What is the purpose of pointers in C programming languages? +# What is the purpose of pointers in C programming languages? -What is the difference between a pointer and a regular variable in C programming languages? +ANSWER -How do you declare a pointer in C programming languages? +Pointers are useful resources. A pointer holds the address of a variable through which the variable being pointed to can be directly manipulated. Both pointer variable and the variable being pointed to must be of the same datatype. -How do you access the value stored at the memory address pointed by a pointer in C programming languages? +# What is the difference between a pointer and a regular variable in C programming languages? -What is a null pointer in C programming languages? +ANSWER -How do you declare pointers to pointers in C programming languages? +The major difference between a regular variable and a pointer variable is that a regular variable passes data by the value stored in it while the pointer passes data by referencing the address of the variable being pointed to. -What is the relationship between pointers and arrays in C programming languages? +--- -What is dynamic memory allocation in C programming languages and how is it achieved using pointers? +# How do you declare a pointer in C programming languages? -How do you deallocate dynamically allocated memory in C programming languages? \ No newline at end of file +ANSWER + +To declare a pointer in C programming language, + +`datatype * pointername;` + +e.g `int * ptr;` + +To avoid gibberish values it is good to initialize your pointer: + +`int * ptr = NULL;` + +but to make it point to a variable or memory location + +`int * ptr = &variablename;` + +--- + +# How do you access the value stored at the memory address pointed by a pointer in C programming languages? + +ANSWER + +You maken use of the asterisk +e.g +```c +int * ptr = # +printf("%d", *ptr); // this will print the value in num; +``` + +--- + +# What is a null pointer in C programming languages? + +ANSWER + +Null pointers are pointers that have been declared but not pointing to a particular memory location or address. They are initialized with the value NULL. + +--- + +# How do you declare pointers to pointers in C programming languages? + +ANSWER + +You declare pointer to pointers using double asterisks +e.g + +`int **ptr;` + +--- + +# What is the relationship between pointers and arrays in C programming languages? + +ANSWER + +The relationship between a pointer and an array is that both hold addresses to a location or memory space. +A pointer holds the address of the variable it is pointing to while the arrayname serves as a pointer to the memory space it has been allocated to keep for a particular size of element of the same datatype. + +--- + +# What is dynamic memory allocation in C programming languages and how is it achieved using pointers? + +ANSWER + +Dynamic allocation of memory is allocating memory at the time of execution (run time). The functions `calloc() and malloc()` support allocating of dynamic memory. + +--- + +# How do you deallocate dynamically allocated memory in C programming languages? + +ANSWER + +`free(variable);` diff --git a/TASKS/Task-22.c b/TASKS/Task-22.c index 8cd01c4..e7cd756 100644 --- a/TASKS/Task-22.c +++ b/TASKS/Task-22.c @@ -2,4 +2,18 @@ Create a program that declares a pointer to an integer, assigns an address to it, and outputs the value stored at the memory address pointed by the pointer. -*/ \ No newline at end of file +*/ +#include +int main(void) +{ + int num = 123; + int *ptr = NULL; + + printf("Number is :\t%d\n", num); + + ptr = # + + printf("Value in the variable pointed to by the pointer is %d\n", *ptr); + + return (0); +} diff --git a/TASKS/Task-23.c b/TASKS/Task-23.c index dfddb16..544a7b4 100644 --- a/TASKS/Task-23.c +++ b/TASKS/Task-23.c @@ -1,4 +1,24 @@ /* Create a program that uses pointers to find the maximum value stored in an array. -*/ \ No newline at end of file +*/ +#include + +int main(void) +{ + int num[] = {12, 32, 2, 8, 10, 405, 7, 15, 35, 117}; + int max = 0, len = 0; + int itr; + int *ptr = num; + + len = sizeof(num) / sizeof(num[0]); + max = num[0]; + for (itr = 1; itr < len; itr++) + { + printf("max: %d\tptr: %d\n", max, *(ptr + itr)); + if (*(ptr + itr) > max) + max = *(ptr + itr); + } + printf("The biggest integer in the array is %d\n", max); + return 0; +} diff --git a/TASKS/Task-24.c b/TASKS/Task-24.c index 79d1c16..64b2a23 100644 --- a/TASKS/Task-24.c +++ b/TASKS/Task-24.c @@ -1,4 +1,45 @@ /* Create a program that demonstrates the use of pointers to pass information between functions. -*/ \ No newline at end of file +*/ + +#include + +int sum_total(int *tsum, int num, int num2) +{ + int sum = 0; + + sum = num + num2; + *tsum += sum; + return (sum); +} + +int accept_val(char *str) +{ + int num = 0; + printf("Enter %s:\t", str); + if (scanf("%d", &num) == 1) + return (num); + return (0); +} + +int main(void) +{ + int sum_all = 0; + int itr = 0; + int n1, n2, sum; + int itrsize = 0; + + itrsize = accept_val("Loop Size"); + printf("\n"); + for (itr = 1; itr <= itrsize; itr++) + { + sum = 0; + n1 = n2 = 0; + n1 = accept_val("First Number"); + n2 = accept_val("Second Number"); + sum = sum_total(&sum_all, n1, n2); + printf("\n%d + %d = %d\n", n1, n2, sum); + } + printf("\nOverall Sum is %d\n", sum_all); +} diff --git a/TASKS/Task-25.c b/TASKS/Task-25.c index e2aa2a5..33defb4 100644 --- a/TASKS/Task-25.c +++ b/TASKS/Task-25.c @@ -1,3 +1,29 @@ /* Declare two integer variables and swap their values using a pointer. -*/ \ No newline at end of file +*/ +#include + +void swapper(int *n, int *m) +{ + int temp; + + temp = *n; + *n = *m; + *m = temp; +} + +int main(void) +{ + int n_one; + int n_two; + + printf("Enter two (2) numbers separated by a space:\t"); + scanf("%d %d", &n_one, &n_two); + printf("Before Swap\n"); + printf("Number 1:\t%d\nNumber 2:\t%d\n", n_one, n_two); + swapper(&n_one, &n_two); + printf("After Swap\n"); + printf("Number 1:\t%d\nNumber 2:\t%d\n", n_one, n_two); + + return (0); +} diff --git a/TASKS/Task-26.c b/TASKS/Task-26.c index a3188fe..ba476f3 100644 --- a/TASKS/Task-26.c +++ b/TASKS/Task-26.c @@ -1,4 +1,63 @@ /* Write a program that takes an array of scores, and then outputs the array in reverse order. -*/ \ No newline at end of file +*/ + +#include +#include +void reverse_arr(int *arr, int size) +{ + int itr = 0; + + printf("The array ["); + for (itr = 0; itr < size; itr++) + { + printf("%d", arr[itr]); + if (itr < (size - 1)) + printf(", "); + } + printf("] printed in reverse order is ["); + + for (itr = (size - 1); itr >= 0; itr--) + { + printf("%d", arr[itr]); + if (itr > 0) + printf(", "); + } + printf("]\n"); +} + +void accept_arr(void) +{ + int size = 0, itr; + static int arr[20]; + + printf("How many numbers?:\t"); + if (scanf("%d", &size) == 1) + { + if (size <= 1) + { + printf("Your array size cannot be lesser than 1."); + exit(1); + } + else if (size > 20) + { + printf("Your array size cannot be more than 20."); + exit(1); + } + for (itr = 0; itr < size; itr++) + { + printf("Enter a number:\t"); + scanf("%i", &arr[itr]); + } + + reverse_arr(arr, size); + } +} + +int main(void) +{ + accept_arr(); + + return (0); +} \ No newline at end of file diff --git a/TASKS/Task-27.c b/TASKS/Task-27.c index 16e5d59..43327b1 100644 --- a/TASKS/Task-27.c +++ b/TASKS/Task-27.c @@ -1,4 +1,58 @@ /* Write a program that takes an array of scores and outputs the highest score and the lowest score -*/ \ No newline at end of file +*/ +#include +#include + +void sort_array(int *arr, int size) +{ + int low = 0, high = 0, itr = 0; + + low = arr[0]; + high = arr[0]; + for (itr = 1; itr < size; itr++) + { + if (arr[itr] < low) + low = arr[itr]; + if (arr[itr] > high) + high = arr[itr]; + } + + printf("\n\nHighest Score:\t%d\nLowest Score:\t%d", high, low); +} + +void arr_high_low_score(void) +{ + int size = 0, itr; + static int arr[20]; + + printf("Class Size?:\t"); + if (scanf("%d", &size) == 1) + { + if (size < 1) + { + printf("Your class size cannot be lesser than 1."); + exit(1); + } + else if (size > 20) + { + printf("Your class size cannot be more than 20."); + exit(1); + } + for (itr = 0; itr < size; itr++) + { + printf("Enter a score:\t"); + scanf("%i", &arr[itr]); + } + + sort_array(arr, size); + } +} + +int main(void) +{ + arr_high_low_score(); + + return (0); +} \ No newline at end of file diff --git a/TASKS/Task-28.c b/TASKS/Task-28.c index ae109e3..a51b51f 100644 --- a/TASKS/Task-28.c +++ b/TASKS/Task-28.c @@ -3,4 +3,56 @@ Write a program to take input of how many students, calculate the average marks of the class by using an array to store the marks of each student and then using a loop to calculate the sum and average of all the marks. -*/ \ No newline at end of file +*/ +#include +#include +void sum_array_score(int *arr, int size) +{ + int itr = 0; + int total_score = 0; + + printf("\nThe sum of the scores in the array ["); + for (itr = 0; itr < size; itr++) + { + printf("%d", arr[itr]); + if (itr < (size - 1)) + printf(", "); + total_score += arr[itr]; + } + printf("] is = %d", total_score); +} + +void accept_arr(void) +{ + int size = 0, itr; + static int arr[20]; + + printf("How many numbers?:\t"); + if (scanf("%d", &size) == 1) + { + if (size <= 1) + { + printf("Your array size cannot be lesser than 1."); + exit(1); + } + else if (size > 20) + { + printf("Your array size cannot be more than 20."); + exit(1); + } + for (itr = 0; itr < size; itr++) + { + printf("Enter a number:\t"); + scanf("%i", &arr[itr]); + } + + sum_array_score(arr, size); + } +} + +int main(void) +{ + accept_arr(); + + return (0); +} \ No newline at end of file diff --git a/TASKS/dukeson_solution/a.out b/TASKS/dukeson_solution/a.out new file mode 100755 index 0000000..fa6a37b Binary files /dev/null and b/TASKS/dukeson_solution/a.out differ diff --git a/TASKS/dukeson_solution/task1.c b/TASKS/dukeson_solution/task1.c new file mode 100644 index 0000000..db0c1ea --- /dev/null +++ b/TASKS/dukeson_solution/task1.c @@ -0,0 +1,19 @@ +#include + +/* function to find the sum of two numbers */ + +double sum_finder(int first, int second) +{ + return (first + second); +} + +int main(void) +{ + int first = 0, second = 0; + + printf("Enter two (2) numbers separated with a space:\t"); + scanf("%i %i", &first, &second); + printf("The sum of %d and %d = %.2f\n", first, second, sum_finder(first, second)); + + return (0); +} \ No newline at end of file diff --git a/TASKS/dukeson_solution/task10.c b/TASKS/dukeson_solution/task10.c new file mode 100644 index 0000000..2fe53b2 --- /dev/null +++ b/TASKS/dukeson_solution/task10.c @@ -0,0 +1,54 @@ +#include + +/* + Write a function to convert temperature from Celsius to + Fahrenheit and vice versa. +*/ + +/** + * farenheit_to_celius - convert Fahrenheit temperature to Celcius + * @temp: temperature value in celcius (double) + * Return: The Fahrenheit value after computation/conversion + */ + +double farenheit_to_celius(double temp) +{ + double result; + + result = ((temp - 32) * 5) / 9; + return (result); +} + +/** + * farenheit_to_celius - convert Celcius temperature to Fahrenheit + * @temp: temperature value in fahrenheit (double) + * Return: The Celcius value after computation/conversion + */ + +double celius_to_farenheit(double temp) +{ + double result; + + result = (temp * 9 / 5) + 32; + return (result); +} + +int main(void) +{ + double num = 0; + double fah_val; + double cel_val; + + printf("Enter a Temperature in Celcius (*C):\t"); + if (scanf("%lf", &num) == 1) + { + fah_val = celius_to_farenheit(num); + printf("\nConverting %.2f celcius to fahrenheit = %.2f\n", num, fah_val); + + cel_val = farenheit_to_celius(fah_val); + printf("\nConverting %.2f fahrenheit to celcius = %.2f\n", fah_val, cel_val); + return (0); + } + printf("\nYou did not enter a numeric data.\n"); + return (1); +} \ No newline at end of file diff --git a/TASKS/dukeson_solution/task11.c b/TASKS/dukeson_solution/task11.c new file mode 100644 index 0000000..e7620a6 --- /dev/null +++ b/TASKS/dukeson_solution/task11.c @@ -0,0 +1,21 @@ +#include + +/* Create a program to calculate the factorial of a number. */ +int factorial(int num) +{ + if (num == 0) + return (1); + if (num == 1) + return (num); + return (num * factorial(num - 1)); +} + +int main(void) +{ + int num = 0; + + printf("Enter the number:\t"); + if (scanf("%i", &num)) + printf("The factorial of %d! = %d\n", num, factorial(num)); + return (0); +} \ No newline at end of file diff --git a/TASKS/dukeson_solution/task12.c b/TASKS/dukeson_solution/task12.c new file mode 100644 index 0000000..9e6577d --- /dev/null +++ b/TASKS/dukeson_solution/task12.c @@ -0,0 +1,37 @@ +#include +#include + +/* Create a program to check if a number is prime or not.. */ +void is_prime(int num) +{ + int itr = 0; + int flag = 0; + + if (num == 0 || num == 1) + flag = 1; + + for (itr = 2; itr <= (num / 2); itr++) + { + if (num % itr == 0) + { + flag = 1; + break; + } + } + if (flag == 0) + printf("%d is a prime number\n", num); + else + printf("%d is NOT a prime number\n", num); +} + +int main(void) +{ + int num = 0; + + printf("Enter the number:\t"); + if (scanf("%i", &num)) + { + is_prime(num); + } + return (0); +} \ No newline at end of file diff --git a/TASKS/dukeson_solution/task13.c b/TASKS/dukeson_solution/task13.c new file mode 100644 index 0000000..77abfb5 --- /dev/null +++ b/TASKS/dukeson_solution/task13.c @@ -0,0 +1,36 @@ +#include +#include + +/* Write a program to find the sum of all elements in an array. */ + + +int sum_array(int argc, char *argv[]) +{ + int i = 1; + int sum = 0; + + for (i = 1; i <= argc; i++) + { + sum += atoi(argv[i]); + } + return (sum); +} + +int main(int argc, char *argv[]) +{ + int i = 1; + if (argc > 1) + { + printf("The sum of the array ["); + for (i = 1; i < argc; i++) + { + printf("%d", atoi(argv[i])); + if (i < (argc - 1)) + printf(", "); + } + printf("] = %d", sum_array(argc, argv)); + } + else + printf("Please enter some numeric data along side your program name."); + return (0); +} \ No newline at end of file diff --git a/TASKS/dukeson_solution/task14.c b/TASKS/dukeson_solution/task14.c new file mode 100644 index 0000000..b5de8d9 --- /dev/null +++ b/TASKS/dukeson_solution/task14.c @@ -0,0 +1,52 @@ +#include +#include + +/* Create a program to find the average of a set of numbers. */ + + +int sum_array(int argc, char *argv[]) +{ + int i = 1; + int sum = 0; + + for (i = 1; i <= argc; i++) + { + sum += atoi(argv[i]); + } + return (sum); +} + +double get_average(int argc, char *argv[]) +{ + int sum = 0; + int count = 0; + double avg = 0; + + sum = sum_array(argc, argv); + count = argc - 1; + avg = (double)sum / (double)count; + return (avg); +} + +int main(int argc, char *argv[]) +{ + int i = 1; + int count = 0; + + if (argc > 1) + { + count = argc - 1; + printf("The sum of the array ["); + for (i = 1; i < argc; i++) + { + printf("%d", atoi(argv[i])); + if (i < (argc - 1)) + printf(", "); + } + printf("] = %d", sum_array(argc, argv)); + printf(" and the average of the %d numbers is %.2f.\n", count, get_average(argc, argv)); + } + else + printf("Please enter some numeric data along side your program name."); + return (0); +} \ No newline at end of file diff --git a/TASKS/dukeson_solution/task15.c b/TASKS/dukeson_solution/task15.c new file mode 100644 index 0000000..8a98229 --- /dev/null +++ b/TASKS/dukeson_solution/task15.c @@ -0,0 +1,83 @@ +#include +#include + +/* Write a program that implements different mathematical functions such as +finding the factorial of a number, +calculating the power of a number, +and finding the square root of a number. */ + +int get_factorial(int a) +{ + if (a == 0) + return (1); + if (a == 1) + return (a); + return (a * get_factorial(a - 1)); +} + +double get_power(int num, int pow) +{ + int itr; + double pwr = 1.0; + + for (itr = pow; itr > 0; --itr) + pwr *= (double)num; + return (pwr); +} + +int get_square_root(int num) +{ + double sqrroot = 1; + int itr = 0; + while (1) + { + itr++; + sqrroot = (num / sqrroot + sqrroot) / 2; + if (itr == num + 1) + break; + } + return (sqrroot); +} + +int accept_value(void) +{ + int num = 0; + + printf("Enter a number:\t"); + if (scanf("%d", &num) == 1) + return (num); + return (0); +} + +int main(void) +{ + char *options = "Make a Choice:\n1 - [Factorial]\n2 - [Power]\n3 - [Square Root]\n"; + int first, response, second; + + printf("%s", options); + if (scanf("%d", &response) == 1) + { + switch (response) + { + case 1: + first = accept_value(); + printf("\nFactorial of %d! = %d\n", first, get_factorial(first)); + break; + case 2: + first = accept_value(); + second = accept_value(); + printf("\n%d raised to the power of %d = %.0f\n", first, second, get_power(first, second)); + break; + case 3: + first = accept_value(); + printf("\nThe square root of %d = %d\n", first, get_square_root(first)); + break; + default: + printf("\nChoose from the options.\n"); + break; + } + } + else + printf("\nNumeric data required.\n"); + return (0); +} \ No newline at end of file diff --git a/TASKS/dukeson_solution/task16.c b/TASKS/dukeson_solution/task16.c new file mode 100644 index 0000000..0523f4b --- /dev/null +++ b/TASKS/dukeson_solution/task16.c @@ -0,0 +1,32 @@ +#include +#include + +#define BUFSIZE 1024 + +/* Write a program to reverse a string. */ + +void rev_string () +{ + char *str; + int count = 0; + + str = (char *)malloc(sizeof(char) * (BUFSIZE + 1)); + printf("Enter a string of text:\t"); + scanf("%[^\n]%*c", str); + + for (count = 0; str[count] != '\0'; count++) + {} + for (count -= 1; count >= 0; --count) + printf("%c", str[count]); + printf("\n"); + + free(str); +} + + +int main(void) +{ + rev_string(); + + return (0); +} \ No newline at end of file diff --git a/TASKS/dukeson_solution/task17.c b/TASKS/dukeson_solution/task17.c new file mode 100644 index 0000000..1101cc1 --- /dev/null +++ b/TASKS/dukeson_solution/task17.c @@ -0,0 +1,58 @@ +#include +#include + +/* Write a program to find the largest and smallest number in an array. */ + +void sort_array(int *arr, int size) +{ + int low = 0, high = 0, itr = 0; + + low = arr[0]; + high = arr[0]; + for (itr = 1; itr < size; itr++) + { + if (*(arr + itr) < low) + low = *(arr + itr); + if (*(arr + itr) > high) + high = *(arr + itr); + } + + printf("\n\nHighest Value:\t%d\nLowest Value:\t%d", high, low); +} + +void accept_arr(void) +{ + int size = 0, itr; + static int arr[20]; + + printf("How many numbers?:\t"); + if (scanf("%d", &size) == 1) + { + if (size <= 1) + { + printf("Your array size cannot be lesser than 1."); + exit(1); + } + else if (size > 20) + { + printf("Your array size cannot be more than 20."); + exit(1); + } + for (itr = 0; itr < size; itr++) + { + printf("Enter a number:\t"); + scanf("%i", &arr[itr]); + } + + sort_array(arr, size); + } +} + + + +int main(void) +{ + accept_arr(); + + return (0); +} \ No newline at end of file diff --git a/TASKS/dukeson_solution/task18.c b/TASKS/dukeson_solution/task18.c new file mode 100644 index 0000000..6724ae3 --- /dev/null +++ b/TASKS/dukeson_solution/task18.c @@ -0,0 +1 @@ +#include \ No newline at end of file diff --git a/TASKS/dukeson_solution/task2.c b/TASKS/dukeson_solution/task2.c new file mode 100644 index 0000000..1527cee --- /dev/null +++ b/TASKS/dukeson_solution/task2.c @@ -0,0 +1,19 @@ +#include + +/* Write a function to find the largest of two numbers */ + +int get_largest(int a, int b) +{ + return ((a > b) ? a : b); +} + +int main(void) +{ + int first = 0, second = 0; + + printf("Enter two (2) numbers separated with a space:\t"); + scanf("%i %i", &first, &second); + printf("The larger of %d and %d is %d\n", first, second, get_largest(first, second)); + + return (0); +} \ No newline at end of file diff --git a/TASKS/dukeson_solution/task3.c b/TASKS/dukeson_solution/task3.c new file mode 100644 index 0000000..46a8763 --- /dev/null +++ b/TASKS/dukeson_solution/task3.c @@ -0,0 +1,24 @@ +#include + +/* Write a function to find the factorial of a number */ + +int get_factorial(int a) +{ + if (a == 0) + return (1); + if (a == 1) + return (a); + return (a * get_factorial(a - 1)); +} + +int main(void) +{ + int num = 0; + + printf("Enter a number:\t"); + if (scanf("%i", &num) == 1) + printf("%d! is %d\n", num, get_factorial(num)); + else + printf("\nYou did not provide a numeric data.\n"); + return (0); +} \ No newline at end of file diff --git a/TASKS/dukeson_solution/task4.c b/TASKS/dukeson_solution/task4.c new file mode 100644 index 0000000..3334b27 --- /dev/null +++ b/TASKS/dukeson_solution/task4.c @@ -0,0 +1,24 @@ +#include + +/* Write a function to check if a number is even or odd */ + +void odd_even(int num) +{ + printf("%s",(num % 2 == 0) ? "Even" : "Odd"); +} + +int main(void) +{ + int num = 0; + + printf("Enter a number:\t"); + if (scanf("%i", &num) == 1) + { + printf("%d is an ", num); + odd_even(num); + printf(" number\n"); + } + else + printf("\nYou did not provide a numeric data.\n"); + return (0); +} \ No newline at end of file diff --git a/TASKS/dukeson_solution/task5.c b/TASKS/dukeson_solution/task5.c new file mode 100644 index 0000000..84a1905 --- /dev/null +++ b/TASKS/dukeson_solution/task5.c @@ -0,0 +1,36 @@ +#include + +/* Write a function to find the GCD (greatest common divisor) of two numbers */ + +int gcd_one(int num_one, int num_two) +{ + if (num_two != 0) + return (gcd_one(num_two, num_one % num_two)); + else + return (num_one); +} + +int gcd_two(int numone, int numtwo) +{ + int i = 0; + int gcd = 0; + + for(i = 1; i <= numone && i <= numtwo; ++i) + { + if(numone % i == 0 && numtwo % i == 0) + gcd = i; + } + return (gcd); +} + +int main(void) +{ + int first = 0, second = 0; + + printf("Enter two (2) numbers separated with a space:\t"); + scanf("%i %i", &first, &second); + printf("\n\nFIRST METHOD\n"); + printf("Greatest Common Divisor of %d and %d:\t %d", first, second, gcd_two(first, second)); + printf("\n\nRECURSIVE METHOD\n"); + printf("Greatest Common Divisor of %d and %d:\t %d", first, second, gcd_one(first, second)); +} diff --git a/TASKS/dukeson_solution/task6.c b/TASKS/dukeson_solution/task6.c new file mode 100644 index 0000000..5b628e8 --- /dev/null +++ b/TASKS/dukeson_solution/task6.c @@ -0,0 +1,26 @@ +#include + +/* Write a function to find the LCM (least common multiple) of two numbers */ + +int get_lcm(int num_one, int num_two) +{ + int biggest = (num_one > num_two) ? num_one : num_two; + + while (1) + { + if (((biggest % num_one) == 0) && ((biggest % num_two) == 0)) + return (biggest); + biggest++; + } + return (0); +} + +int main(void) +{ + int first = 0, second = 0; + + printf("Enter two (2) numbers separated with a space:\t"); + scanf("%i %i", &first, &second); + printf("The LCM of %d and %d = %d\n", first, second, get_lcm(first, second)); + return (0); +} \ No newline at end of file diff --git a/TASKS/dukeson_solution/task7.c b/TASKS/dukeson_solution/task7.c new file mode 100644 index 0000000..757e2a6 --- /dev/null +++ b/TASKS/dukeson_solution/task7.c @@ -0,0 +1,22 @@ +#include + +#define PI 3.14159 + +/* Write a function to find the area of a circle */ + +double get_circle_area(int radius) +{ + if (radius > 0) + return ((double)(PI * (radius * radius))); + return (0.0); +} + +int main(void) +{ + int rad = 0; + + printf("Enter the radius:\t"); + if (scanf("%i", &rad)) + printf("The area of a circle with radius of %d = %.2f\n", rad, get_circle_area(rad)); + return (0); +} \ No newline at end of file diff --git a/TASKS/dukeson_solution/task8.c b/TASKS/dukeson_solution/task8.c new file mode 100644 index 0000000..63e6673 --- /dev/null +++ b/TASKS/dukeson_solution/task8.c @@ -0,0 +1,22 @@ +#include + +#define PI 3.14159 + +/* Write a function to find the volume of a sphere */ + +double sphere_volume(int radius) +{ + if (radius > 0) + return ((double)(4 * (PI * (radius * radius * radius))) / 3); + return (0.0); +} + +int main(void) +{ + int rad = 0; + + printf("Enter the radius:\t"); + if (scanf("%i", &rad)) + printf("The volume of a sphere with radius of %d = %.2f\n", rad, sphere_volume(rad)); + return (0); +} \ No newline at end of file diff --git a/TASKS/dukeson_solution/task9.c b/TASKS/dukeson_solution/task9.c new file mode 100644 index 0000000..65e11fb --- /dev/null +++ b/TASKS/dukeson_solution/task9.c @@ -0,0 +1,39 @@ +#include + +/* Write a function to find the Fibonacci series of a given number */ +void print_fibo_series(int num) +{ + int temp; + int swp_1 = 0, swp_2 = 1; + int itr = 0; + + if (num >= 2) + { + printf("%d %d ", swp_1, swp_2); + while (itr < num) + { + temp = swp_1 + swp_2; + printf("%d ", temp); + swp_1 = swp_2; + swp_2 = temp; + ++itr; + } + } + else + { + printf("%d", swp_1); + } +} + +int main(void) +{ + int num = 0; + + printf("Enter the radius:\t"); + if (scanf("%i", &num)) + { + printf("Fibonnaci series for %d = ", num); + print_fibo_series(num); + } + return (0); +} \ No newline at end of file diff --git a/TASKS/dukeson_solution/task_list.md b/TASKS/dukeson_solution/task_list.md new file mode 100644 index 0000000..6444e23 --- /dev/null +++ b/TASKS/dukeson_solution/task_list.md @@ -0,0 +1,34 @@ +Write a function to find the sum of two numbers +Write a function to find the largest of two numbers +Write a function to find the factorial of a number +Write a function to check if a number is even or odd +Write a function to find the GCD (greatest common divisor) of two numbers +Write a function to find the LCM (least common multiple) of two numbers +Write a function to find the area of a circle +Write a function to find the volume of a sphere +Write a function to find the Fibonacci series of a given number +Write a function to convert temperature from Celsius to Fahrenheit and vice versa. +Write a program that implements different mathematical functions such as finding the factorial of a number, calculating the power of a number, and finding the square root of a number. +Write a program that implements functions to perform string operations, such as concatenating two strings, finding the length of a string, and reversing a string. +Write a program that implements functions to perform operations on arrays, such as finding the largest and smallest elements in an array, sorting an array in ascending or descending order, and finding the average of all elements in an array. +Write a program that implements functions to perform operations on structures, such as finding the largest and smallest elements in a structure, sorting a structure in ascending or descending order, and finding the average of all elements in a structure. +Create a program to calculate the factorial of a number. +Write a program to find the largest and smallest number in an array. +Create a program to sort an array of numbers in ascending or descending order. +Write a program to reverse a string. +Create a program to check if a number is prime or not. +Write a program to find the sum of all elements in an array. +Create a program to find the average of a set of numbers. +Write a program to find the frequency of each character in a string. +Create a program to convert a decimal number to binary, octal, or hexadecimal. +Write a program to check if a string is a palindrome. +Write a program to implement the QuickSort algorithm. +Create a program to implement the MergeSort algorithm. +Write a program to find the Longest Common Subsequence (LCS) of two strings. +Create a program to find the shortest path in a graph using Dijkstra's algorithm. +Write a program to implement the Binary Search algorithm. +Create a program to find the minimum number of coins needed to make a certain amount of change. +Write a program to implement the Knapsack problem. +Create a program to implement the Prim's algorithm for finding the minimum spanning tree in a graph. +Write a program to implement the Tower of Hanoi puzzle. +Create a program to implement the Depth-First Search (DFS) algorithm for graph traversal. \ No newline at end of file