From 0adb5b59ffe88f4e15e2fba55c61750a9f9341c0 Mon Sep 17 00:00:00 2001 From: Abdulazeez ibrahim Date: Wed, 25 Jan 2023 23:07:40 -0800 Subject: [PATCH 1/4] learning functions --- TASKS/Task 002.c | 24 +++++++++++++++++++++++- TASKS/Task 003.c | 32 +++++++++++++++++++++++++++++++- TASKS/Task 004.c | 21 ++++++++++++++++++++- TASKS/Task 005.c | 16 +++++++++++++++- TASKS/Task 006.c | 23 ++++++++++++++++++++++- TASKS/Task 007.c | 22 +++++++++++++++++++++- TASKS/Task 008.c | 21 ++++++++++++++++++++- 7 files changed, 152 insertions(+), 7 deletions(-) diff --git a/TASKS/Task 002.c b/TASKS/Task 002.c index 5cdd390..e51fcb5 100644 --- a/TASKS/Task 002.c +++ b/TASKS/Task 002.c @@ -3,4 +3,26 @@ 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 +*/ +int main() +{ + int check; + + printf("What is your score ?"); + scanf("%i", &check);// take user score + user_score(check); + return (0); +} + +void user_score(int score) +{ + // check if score is less than 80 + if (score < 80) + { + printf("You are not eligible"); + } + else// above 80 + { + printf("You are eligible"); + } +} \ No newline at end of file diff --git a/TASKS/Task 003.c b/TASKS/Task 003.c index 7fca79c..3dfecb8 100644 --- a/TASKS/Task 003.c +++ b/TASKS/Task 003.c @@ -6,4 +6,34 @@ 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 +*/ +void read_int(int num) +{ + // check if num is even + if (num % 2 == 0) + { + printf("%i is an even number\n", num); + } + else if (num % 2 == 1) + { + printf("%i is an odd number\n", num); + } + else if (num == 0) + { + printf("The number is Zero (%i)\n", num); + } + else + { + printf("unknown input!\n"); + } +} + +int main(void) +{ + int check; + + printf("Choose your lucky number!: "); + scanf("%i", &check); + read_int(check); + return (0); +} \ No newline at end of file diff --git a/TASKS/Task 004.c b/TASKS/Task 004.c index 17ee38c..3f0b474 100644 --- a/TASKS/Task 004.c +++ b/TASKS/Task 004.c @@ -11,4 +11,23 @@ How old are you?: 65 Output: Your name is David and you are 65 years old. -*/ \ No newline at end of file +*/ +// char user_profile(char name, int age) +// { +// printf("Your name is %s and you are %i years old!\n", name, age); +// } + +int main(void) +{ + int age; + char name[100]; + + printf("What is your name?"); + scanf("%s", name); + printf("How old are you?"); + scanf("%i", &age); + // user_profile(name,age); + printf("Your name is %s and you are %i years old!\n", name, age); + return (0); + +} \ No newline at end of file diff --git a/TASKS/Task 005.c b/TASKS/Task 005.c index db32ab3..c86a466 100644 --- a/TASKS/Task 005.c +++ b/TASKS/Task 005.c @@ -1,4 +1,18 @@ /* Write a program in C to find the square of any number using the function. -*/ \ No newline at end of file +*/ +int sq_num(x) +{ + return (x * x); +} + +int main(void) +{ + int x; + + printf("input a number: "); + scanf("%i", &x); + printf("The square of %i is %i", x, sq_num(x)); + return (0); +} \ No newline at end of file diff --git a/TASKS/Task 006.c b/TASKS/Task 006.c index dc0b9af..6b5eb5d 100644 --- a/TASKS/Task 006.c +++ b/TASKS/Task 006.c @@ -1,4 +1,25 @@ /* Write a program in C to check a given number is even or odd using the function. -*/ \ No newline at end of file +*/ +int main(void) +{ + int num; + + printf("Input a number \n"); + scanf("%i", &num); + even_odd(num); + return (0); +} + +void even_odd(int check) +{ + if (check % 2 == 0) + { + printf("This an even number"); + } + else + { + printf("This is an odd number"); + } +} \ No newline at end of file diff --git a/TASKS/Task 007.c b/TASKS/Task 007.c index 07ea0fb..f3ac89f 100644 --- a/TASKS/Task 007.c +++ b/TASKS/Task 007.c @@ -2,4 +2,24 @@ 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 print_abc(char abc) +{ + abc = 'A'; + + while (abc <= 'Z') + { + printf("%c ", abc); + abc++; + } + printf("\n"); +} + +int main(void) +{ + char abc = 'A'; + + print_abc(abc); + return (0); +} \ No newline at end of file diff --git a/TASKS/Task 008.c b/TASKS/Task 008.c index 0a815b2..52ba00d 100644 --- a/TASKS/Task 008.c +++ b/TASKS/Task 008.c @@ -1,4 +1,23 @@ /*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 print_abc(char abc) +{ + abc = 'a'; + + while (abc <= 'z') + { + printf("%c ", abc); + abc++; + } + printf("\n"); +} + +int main(void) +{ + char abc = 'a'; + + print_abc(abc); + return (0); +} \ No newline at end of file From 4136a585f283378c395025ca5c3f309888271c4d Mon Sep 17 00:00:00 2001 From: Abdulazeez ibrahim Date: Sun, 29 Jan 2023 03:54:34 -0800 Subject: [PATCH 2/4] functions --- TASKS/.gitignore | 2 ++ TASKS/Task 001.c | 16 +++++++++++++++- TASKS/Task 002.c | 1 + TASKS/Task 003.c | 1 + TASKS/Task 009.c | 20 +++++++++++++++++++- TASKS/Task 010.c | 18 +++++++++++++++++- TASKS/Task 011.c | 23 ++++++++++++++++++++++- TASKS/Task 012.c | 13 ++++++++++++- TASKS/Task 013.c | 20 +++++++++++++++++++- TASKS/Task 014.c | 18 +++++++++++++++++- TASKS/Task 015.c | 32 +++++++++++++++++++++++++++++++- 11 files changed, 156 insertions(+), 8 deletions(-) create mode 100644 TASKS/.gitignore diff --git a/TASKS/.gitignore b/TASKS/.gitignore new file mode 100644 index 0000000..1ce788f --- /dev/null +++ b/TASKS/.gitignore @@ -0,0 +1,2 @@ +*.exe +*.o \ No newline at end of file diff --git a/TASKS/Task 001.c b/TASKS/Task 001.c index 24be041..8d90371 100644 --- a/TASKS/Task 001.c +++ b/TASKS/Task 001.c @@ -4,4 +4,18 @@ Write a program that prints the alphabet in lowercase, followed by a new line. Print all the letters except q and e use printf -*/ \ No newline at end of file +*/ +#include + +int main(void) +{ + char alpha; + + for (alpha = 'a'; alpha <= 'z'; alpha++) + { + if (alpha != 'e' && alpha != 'q') + printf("%c ", alpha); + } + printf("\n"); + return(0); +} diff --git a/TASKS/Task 002.c b/TASKS/Task 002.c index e51fcb5..767c2cd 100644 --- a/TASKS/Task 002.c +++ b/TASKS/Task 002.c @@ -4,6 +4,7 @@ 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. */ +#include int main() { int check; diff --git a/TASKS/Task 003.c b/TASKS/Task 003.c index 3dfecb8..0235a22 100644 --- a/TASKS/Task 003.c +++ b/TASKS/Task 003.c @@ -7,6 +7,7 @@ If the number is zero, display 'This is zero' NOTE: 'userInput' should be the number entered by the user. */ +#include void read_int(int num) { // check if num is even diff --git a/TASKS/Task 009.c b/TASKS/Task 009.c index d481f90..0162a15 100644 --- a/TASKS/Task 009.c +++ b/TASKS/Task 009.c @@ -2,4 +2,22 @@ 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 +*/ +#include +void _islower(char low) +{ + if (low >= 'a' && low <= 'z' ) + printf("%c - This is in lowercase", low); + else + printf("This is in uppercase"); +} + +int main(void) +{ + char let; + + printf("input an argument\n"); + scanf("%c", &let); + _islower(let); + return(0); +} diff --git a/TASKS/Task 010.c b/TASKS/Task 010.c index 62c07e9..e39fb99 100644 --- a/TASKS/Task 010.c +++ b/TASKS/Task 010.c @@ -2,4 +2,20 @@ 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 +*/ +#include +int abs(int x) +{ + int y; + + y = y * -1; + return(y); +} + +int main(void) +{ + int z = -3; + + abs(z); + return(0); +} \ No newline at end of file diff --git a/TASKS/Task 011.c b/TASKS/Task 011.c index 96c4add..7da7a49 100644 --- a/TASKS/Task 011.c +++ b/TASKS/Task 011.c @@ -6,4 +6,25 @@ Output should look like this; 9 x 1 = 9 9 x 2 = 18 ... and so on till 12 -*/ \ No newline at end of file +*/ +#include +void times_table(int times) +{ + int i, j; + + for (i = times; i <= times; i++) // outer loop + { + for (j = 1; j < 12; j++) //inner loop + { + printf("%02d * %02d = %03d\n", i, j,(i * j)); + } + printf("\n"); + } +} +int main(void) +{ + int num; + printf("print time table of "); + scanf("%d", &num); + times_table(num); +} diff --git a/TASKS/Task 012.c b/TASKS/Task 012.c index 735a015..21fc024 100644 --- a/TASKS/Task 012.c +++ b/TASKS/Task 012.c @@ -1,4 +1,15 @@ /*Write your solution after the comment Write a function that adds two integers and prints the result. -*/ \ No newline at end of file +*/ +#include +int add_int(int x, int y) +{ + return(x + y); +} + +int main(void) +{ + add_int(4, 5); + printf("the addition of %d and %d is %d", 4, 5, add_int(4,5)); +} \ No newline at end of file diff --git a/TASKS/Task 013.c b/TASKS/Task 013.c index 068a6cc..335fbf9 100644 --- a/TASKS/Task 013.c +++ b/TASKS/Task 013.c @@ -3,4 +3,22 @@ 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 +*/ +#include +void natuaral_num(int nat) +{ + int i; + for (i = nat; i <= 100; i++) // start from the input + { + printf("%i ", i); + } + printf("\n"); +} +int main(void) +{ + int n; + + printf("Printing of natural numbers. Choose a number!\n"); + scanf("%i", &n); + natuaral_num(n); +} \ No newline at end of file diff --git a/TASKS/Task 014.c b/TASKS/Task 014.c index d139cd2..ba09a45 100644 --- a/TASKS/Task 014.c +++ b/TASKS/Task 014.c @@ -6,4 +6,20 @@ 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 +*/ +#include +int main(void) +{ + int i, j; + + for (i = 0; i < 10; i++) // runs 10 times outer loop + { + for (j = 0; j < 15; j++) // inner loop + { + printf("%d ", j); // prints from 0 - 14 + } + printf("\n"); // prints new line after each iteration + } + printf("\n"); // prints new line after the loop is done + return(0); +} diff --git a/TASKS/Task 015.c b/TASKS/Task 015.c index 7cac93a..210a27e 100644 --- a/TASKS/Task 015.c +++ b/TASKS/Task 015.c @@ -6,4 +6,34 @@ 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 +*/ +#include +int main(void){ + fizz_buzz(); + return(0); +} + +void fizz_buzz() +{ + int i; + + for ( i = 1; i <= 100; i++) + { + /* code */ + if (i % 3 == 0) + { + printf("Fizz "); + } + else if (i % 5 == 0) + { + printf("Buzz "); + } + else if (i % 3 == 0 && i % 5) + { + printf("FizzBuzz "); + } + else + printf("%d ", i); + } + printf("\n"); +} \ No newline at end of file From f91072fbd955f72f1116aecbcfd1f71d1eac24c7 Mon Sep 17 00:00:00 2001 From: tech2world Date: Sat, 4 Feb 2023 10:26:01 -0800 Subject: [PATCH 3/4] recursive functions --- TASKS/Task 016.c | 21 ++++++++++++++++++++- TASKS/Task 017.c | 21 ++++++++++++++++++++- TASKS/Task 018.c | 23 ++++++++++++++++++++++- TASKS/Task 019.c | 18 +++++++++++++++++- 4 files changed, 79 insertions(+), 4 deletions(-) diff --git a/TASKS/Task 016.c b/TASKS/Task 016.c index 8700bc0..e5d2d85 100644 --- a/TASKS/Task 016.c +++ b/TASKS/Task 016.c @@ -8,4 +8,23 @@ Output: 720 // That is 1x2x3x4x5x6 = 720 -*/ \ No newline at end of file +*/ +int recur_prod(int k) +{ + if (k == 0) + return (1); + else + return (k * recur_prod(k - 1); +} + +int main(void) +{ + int n, prod; + + printf("Enter an integer "); + scanf("%d", &n); + + prod = recur_prod(n); + printf("The product is %d\n", prod); + return (0); +} diff --git a/TASKS/Task 017.c b/TASKS/Task 017.c index f5ef778..e51195a 100644 --- a/TASKS/Task 017.c +++ b/TASKS/Task 017.c @@ -8,4 +8,23 @@ Output: 21 // That is; 1+2+3+4+5+6 = 21 -*/ \ No newline at end of file +*/ +int recur_add(int k) +{ + if (k == 0) + return (0); + else + return (k + recur_add(k - 1); +} + +int main(void) +{ + int n, add; + + printf("Enter an integer\n"); + scanf("%d", &n); + + add = recur_add(n); + printf("The addition is %d\n", add); + return (0); +} diff --git a/TASKS/Task 018.c b/TASKS/Task 018.c index 147ff9a..264a3eb 100644 --- a/TASKS/Task 018.c +++ b/TASKS/Task 018.c @@ -2,4 +2,25 @@ 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 recur_pow(int x, int y) +{ + if (y == 0) + return (1); + else + return (x * (recur_pow(x, (y - 1))); +} + +int main(void) +{ + int base, pow, ans; + + printf("Enter Base\n"); + scanf("%d", &base); + printf("Enter Power\n"); + scanf("%d", &pow); + + ans = recur_pow(base,pow) + printf("The result of %d raised to %d is %d", base, pow, ans); + return (0); +} diff --git a/TASKS/Task 019.c b/TASKS/Task 019.c index 5cc5874..cce8b05 100644 --- a/TASKS/Task 019.c +++ b/TASKS/Task 019.c @@ -1,4 +1,20 @@ /* Write a function to convert temperature from Celsius to Fahrenheit and vice versa. -*/ \ No newline at end of file +*/ +double cels_to_fah(double temp) +{ + return ((9.0 / 5.0) * temp + 32); +} + +int main(void) +{ + double temp, fahr; + + printf("Enter the temperature in celsius: "); + scanf("%lf", &temp); + + fahr = cels_to_fah(temp); + printf("This is the temp in fahrenheit: %lf\n", fahr); + return (0); +} From 092098995a347ac05597c110737d4bc1a8f1b9e5 Mon Sep 17 00:00:00 2001 From: Abdulazeez ibrahim Date: Mon, 13 Feb 2023 23:19:44 -0800 Subject: [PATCH 4/4] updated tasks --- TASKS/Task 017.c | 2 +- TASKS/Task 018.c | 5 +++-- TASKS/Task-22.c | 15 ++++++++++++++- TASKS/Task-23.c | 28 +++++++++++++++++++++++++++- TASKS/Task-24.c | 24 +++++++++++++++++++++++- TASKS/Task-25.c | 30 +++++++++++++++++++++++++++++- 6 files changed, 97 insertions(+), 7 deletions(-) diff --git a/TASKS/Task 017.c b/TASKS/Task 017.c index e51195a..7a27763 100644 --- a/TASKS/Task 017.c +++ b/TASKS/Task 017.c @@ -14,7 +14,7 @@ int recur_add(int k) if (k == 0) return (0); else - return (k + recur_add(k - 1); + return (k + recur_add(k - 1)); } int main(void) diff --git a/TASKS/Task 018.c b/TASKS/Task 018.c index 264a3eb..4715de1 100644 --- a/TASKS/Task 018.c +++ b/TASKS/Task 018.c @@ -3,12 +3,13 @@ functionName(int x, int y); */ +#include int recur_pow(int x, int y) { if (y == 0) return (1); else - return (x * (recur_pow(x, (y - 1))); + return (x * (recur_pow(x, (y - 1)))); } int main(void) @@ -20,7 +21,7 @@ int main(void) printf("Enter Power\n"); scanf("%d", &pow); - ans = recur_pow(base,pow) + ans = recur_pow(base,pow); printf("The result of %d raised to %d is %d", base, pow, ans); return (0); } diff --git a/TASKS/Task-22.c b/TASKS/Task-22.c index 8cd01c4..a07ef2f 100644 --- a/TASKS/Task-22.c +++ b/TASKS/Task-22.c @@ -2,4 +2,17 @@ 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() +{ + int *ptr; // pointer + int num = 91; // variable that hold an int + + ptr = # // assigning the address of the int to the pointer + + printf("This is the address: %p\n", ptr);// prints address using pointer + printf("This is the value: %d\n", *ptr); // print value using pointer + return 0; +} + diff --git a/TASKS/Task-23.c b/TASKS/Task-23.c index dfddb16..be45341 100644 --- a/TASKS/Task-23.c +++ b/TASKS/Task-23.c @@ -1,4 +1,30 @@ /* 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 i; // number of elements in array + int j; // index of element in array + int array[100]; + + printf("enter number of elements in array (from 1-100): "); + scanf("%d", &i); + + for (j = 0; j < i; ++j) + { + printf("Enter %d number: ", j + 1); + scanf("%d", &array[j]); + } + //to get the largest number + for (j = 0; j < i; ++j)// loop through the indices of the array + { + if (array[0] < array[j])// if the array[0] is smaller than the current array[j] + { + array[0] = array[j]; // update the array[0] + } + } + printf("Largest number is: %d\n", array[0]); + return (0); +} \ No newline at end of file diff --git a/TASKS/Task-24.c b/TASKS/Task-24.c index 79d1c16..6377b18 100644 --- a/TASKS/Task-24.c +++ b/TASKS/Task-24.c @@ -1,4 +1,26 @@ /* Create a program that demonstrates the use of pointers to pass information between functions. -*/ \ No newline at end of file +*/ +// write a program that adds elements of an array and outputs the sum +#include +int main(void) +{ + int arr[4] = {10, 20, 30, 40}; + // total = + + printf("The total of all the number is: %d", add_arr(arr, 4)); +} + +int add_arr(int *ptr, int n) +{ + int i = 0; + int sum = 0; + + while (i < n) + { + sum = sum + *ptr; + i++; + } + return (sum); +} \ No newline at end of file diff --git a/TASKS/Task-25.c b/TASKS/Task-25.c index e2aa2a5..793ca53 100644 --- a/TASKS/Task-25.c +++ b/TASKS/Task-25.c @@ -1,3 +1,31 @@ /* Declare two integer variables and swap their values using a pointer. -*/ \ No newline at end of file +*/ +#include + +int add_arr(int *ptr, int n); + +int main(void) +{ + int arr[4] = {10, 20, 30, 40}; + + printf("The total of all the number is: %d", add_arr(arr, 4)); +} +/** add_arr - sums up all the elements in the array and return the output + * @ptr: pointer to the array + * @n: number of elements in the array + * Return: sum + */ + +int add_arr(int *ptr, int n) +{ + int i = 0; + int sum = 0; // initialize to 0 + + while (i < n) + { + sum += ptr[i]; // add value of pointer to sum + i++; + } + return (sum); +}