diff --git a/06-conditional-statements.c b/06-conditional-statements.c index b095295..8a8a073 100644 --- a/06-conditional-statements.c +++ b/06-conditional-statements.c @@ -17,6 +17,22 @@ Practice Program Check if a number is negative */ +#include + +int main() +{ + int n; + printf("Enter a number\n"); + scanf("%d", &n); + + if (n < 0) + { + printf("%d is a negative number\n", n); + } + + + + /* C if...else Statement @@ -42,6 +58,23 @@ Practice Program Check whether a number is odd or even */ + + int p; + printf("Enter a number\n"); + scanf("%d", &p); + + if (p % 2 == 0) + { + printf("%d is an even number\n", p); + } + + else + { + printf("%d is an odd number\n", p); + } +return (0); +} + /* C if...else Ladder The if...else statement executes two different codes depending @@ -70,4 +103,10 @@ else { Practice Program Program to relate two integers using ==, > or < symbol -*/ \ No newline at end of file + + +=*=*=*=*=*= Please explain this practice task =*=*=*=*=*=*= + + + +*/ diff --git a/07-loops.c b/07-loops.c index 72ea267..8d385de 100644 --- a/07-loops.c +++ b/07-loops.c @@ -33,6 +33,20 @@ Print Numbers 1 - 10 Calculate the sum */ +#include + +int main() +{ + int sum = 0; + + for ( int i = 1; i <= 10; ++i) + { + printf("%d ", i); + sum += i; + } + + printf("\nsum = %d\n", sum); + /* C while loop The syntax of the while loop is: @@ -51,6 +65,14 @@ Practice Program Print Numbers 1 - 10 */ +int n = 0; + + while (n < 10) + { + ++n; + printf("%d ", n); + } + /* do...while loop The do..while loop is similar to the while loop with one important @@ -72,4 +94,24 @@ How do...while loop works? Practice Program Ask for correct input continously -*/ \ No newline at end of file +*/ + + +int num; + printf("Enter a number from 1 to 3\n"); + scanf("%d", &num); + + do + { + printf("Enter the correct input\n"); + scanf("%d", &num); + } + while (num > 3); + + if (num < 3) + printf("%d is correct", num); + + +return (0); + +} diff --git a/08-Ternary-Operator.c b/08-Ternary-Operator.c index 6d968b6..4264d6b 100644 --- a/08-Ternary-Operator.c +++ b/08-Ternary-Operator.c @@ -16,4 +16,18 @@ Syntax condition ? value_if_true : value_if_false Ternary operator can also be nested -*/ \ No newline at end of file +*/ + +#include + +int main() +{ + int num; + + printf("Enter a number\n"); + scanf("%d",&num); + + num % 2 == 0 ? printf("%d is an even number", num) : printf("%d is an odd number", num); + +return (0); +} diff --git a/09-Nested-Loops.c b/09-Nested-Loops.c index 2b01945..b1fa34c 100644 --- a/09-Nested-Loops.c +++ b/09-Nested-Loops.c @@ -34,6 +34,32 @@ Practice Problem FizzBuzz */ + +#include + +int main(void) +{ + int i; + + for (i=1; i<=100; i++) + { + if (i%15 == 0) + printf ("FizzBuzz\t"); + + else if (i%3 == 0) + printf("Fizz\t"); + + else if (i%5 == 0) + printf("Buzz\t"); + + else + printf("%d\t", i); + } + + return 0; +} + + /* int n = 6;// variable declaration //printf("Enter the value of n :"); @@ -46,4 +72,4 @@ int n = 6;// variable declaration } printf("\n"); } -*/ \ No newline at end of file +*/ diff --git a/10-Break-and-Continue.c b/10-Break-and-Continue.c index 0cf3758..0bb8b9a 100644 --- a/10-Break-and-Continue.c +++ b/10-Break-and-Continue.c @@ -44,4 +44,4 @@ for (i = 0; i < 10; i++) { printf("%d\n", i); } -*/ \ No newline at end of file +*/ diff --git a/11-Switch-Statement.c b/11-Switch-Statement.c index 7015891..c9575b4 100644 --- a/11-Switch-Statement.c +++ b/11-Switch-Statement.c @@ -33,4 +33,53 @@ The default clause inside the switch statement is optional. Practice Program Create a calculator -*/ \ No newline at end of file +*/ + +#include + +int main() +{ + + double n1, n2, result; + char opp; + + + scanf("%c", &opp); + scanf("%lf %lf", &n1, &n2); + + /*if (opp != '*' && opp != '+' && opp != '%' && opp != '-') + { + printf("Enter a valid operator"); + scanf("%c", &opp); + }*/ + + + switch (opp) + { + case '+': + result = n1 + n2; + printf("%lf", result); + break; + + + case '-': + result = n1 - n2; + printf("%lf", result); + break; + + case '/': + result = n1 / n2; + printf("%lf", result); + break; + + case '*': + result = n1 * n2; + printf("%lf", result); + break; + + default: + printf("Enter a valid operator"); + } + +return (0); +} diff --git a/TASKS/Task 001.c b/TASKS/Task 001.c index 24be041..412c98f 100644 --- a/TASKS/Task 001.c +++ b/TASKS/Task 001.c @@ -4,4 +4,20 @@ 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() +{ + + + for ( char i = 'a'; i <= 'z' ; i++) + { + if (i != 'e' && i != 'q') + { + printf("%c\n", i); + } + } +return (0); +} diff --git a/TASKS/Task 002.c b/TASKS/Task 002.c index 5cdd390..1e91ebe 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 +*/ + +#include + +int main() +{ + int score; + + printf("Enter your score\n"); + scanf("%d", &score); + + if (score >= 80) + { + printf("You are eligible to enrole"); + } + else + { + printf("you are not eligible to enrole"); + } + + + return (0); +} diff --git a/TASKS/Task 003.c b/TASKS/Task 003.c index 7fca79c..b397eef 100644 --- a/TASKS/Task 003.c +++ b/TASKS/Task 003.c @@ -6,4 +6,36 @@ 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 +*/ + +#include + +int main() +{ +int num; + +printf("Enter a number\n"); +scanf("%d", &num); + + if (num >= 1) + { + if (num % 2 == 0) + { + printf("%d is Odd number", num); + } + else + { + printf("%d is Even number", num); + } + } + else if (num == 0) + { + printf("This is zero"); + } + else + { + printf("\"Error!\" Enter a positive number"); + } + +return (0); +} diff --git a/TASKS/Task 004.c b/TASKS/Task 004.c index 17ee38c..679a679 100644 --- a/TASKS/Task 004.c +++ b/TASKS/Task 004.c @@ -11,4 +11,21 @@ How old are you?: 65 Output: Your name is David and you are 65 years old. -*/ \ No newline at end of file +*/ + + +#include + +int main() +{ + char age[5], name[10]; + + printf("What is your name:\n"); + scanf("%s", &name); + printf("How old are you\n"); + scanf("%s", &age); + + printf("Your name is %s and you are %s years old.", name, age); + +return (0); +} diff --git a/TASKS/Task 005.c b/TASKS/Task 005.c index db32ab3..89769dc 100644 --- a/TASKS/Task 005.c +++ b/TASKS/Task 005.c @@ -1,4 +1,20 @@ /* Write a program in C to find the square of any number using the function. -*/ \ No newline at end of file +*/ + +#include +#include + +int main() +{ + int x; + printf("Enter a number to find the square root\n"); + scanf("%d", &x); + + float res; + res = sqrt(x); + printf (" The square root of %d is: %.2f", x, res); + + return 0; +} diff --git a/TASKS/Task 006.c b/TASKS/Task 006.c index dc0b9af..8d48298 100644 --- a/TASKS/Task 006.c +++ b/TASKS/Task 006.c @@ -1,4 +1,26 @@ /* Write a program in C to check a given number is even or odd using the function. -*/ \ No newline at end of file +*/ + +#include +int checkEven(int a) +{ + + + if (a % 2 == 0) + { + printf("%d is an Even number", a); + } + else + { + printf("%d is an Odd number", a); + } +} +int main() +{ + int num; + printf("Enter a number\n"); + scanf("%d", &num); + + checkEven(num); diff --git a/TASKS/Task 007.c b/TASKS/Task 007.c index 07ea0fb..6813e00 100644 --- a/TASKS/Task 007.c +++ b/TASKS/Task 007.c @@ -2,4 +2,21 @@ 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 +*/ + +#include + +char upperAlpha() +{ + for (char i = 'A'; i <= 'Z'; ++i) + { + printf("%c\n", i); + } +} + +int main() +{ + upperAlpha(); + + return (0); +} diff --git a/TASKS/Task 008.c b/TASKS/Task 008.c index 0a815b2..a7b4269 100644 --- a/TASKS/Task 008.c +++ b/TASKS/Task 008.c @@ -1,4 +1,21 @@ /*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 +*/ + +#include + +char upperAlpha() +{ + for (char i = 'a'; i <= 'z'; ++i) + { + printf("%c\n", i); + } +} + +int main() +{ + upperAlpha(); + + return (0); +} diff --git a/TASKS/Task 009.c b/TASKS/Task 009.c index d481f90..8442036 100644 --- a/TASKS/Task 009.c +++ b/TASKS/Task 009.c @@ -2,4 +2,30 @@ 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 + +char checkCase(char b) +{ + if (b >= 'a' && b <= 'z') + { + printf("This is a lowercase character\n"); + } + else + { + printf("This is not a lowercase character\n"); + } +} + +int main() +{ + char s; + + printf("Enter a character\n"); + scanf("%c", &s); + + checkCase(s); + + return (0); +} diff --git a/TASKS/Task 010.c b/TASKS/Task 010.c index 62c07e9..b48650c 100644 --- a/TASKS/Task 010.c +++ b/TASKS/Task 010.c @@ -2,4 +2,30 @@ 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 absNum(int n) +{ + int abs; + if (n < 0) + { + abs = n - n - n; + printf("The absolute of %d is %d", n, abs); + } + else + { + printf("The absolute of %d is %d", n, n); + } +} + +int main() +{ + int num; + printf("Enter a number:\n"); + scanf("%d", &num); + + absNum(num); + + return (0); +} diff --git a/TASKS/Task 011.c b/TASKS/Task 011.c index 96c4add..d26246e 100644 --- a/TASKS/Task 011.c +++ b/TASKS/Task 011.c @@ -6,4 +6,19 @@ 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 + +int nineTable() +{ + for (int i = 0; i <= 12; ++i) + { + printf("9 x %d = %d\n", i, 9 * i); + } +} + +int main() +{ + nineTable(); +} diff --git a/TASKS/Task 012.c b/TASKS/Task 012.c index 735a015..f82cbc3 100644 --- a/TASKS/Task 012.c +++ b/TASKS/Task 012.c @@ -1,4 +1,22 @@ /*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 a, int b) +{ + int sum; + sum = a + b; + printf("%d + %d = %d\n", a, b, sum); +} + +int main() +{ + int n1, n2; + printf("Enter two numbers to add\n"); + scanf("%d \n %d", &n1, &n2); + + add(n1, n2); +} diff --git a/TASKS/Task 013.c b/TASKS/Task 013.c index 068a6cc..21e895c 100644 --- a/TASKS/Task 013.c +++ b/TASKS/Task 013.c @@ -3,4 +3,28 @@ 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 + +int natNum(int n) +{ + for (int i = 0; i <= 100; ++i) + { + if (n <= i) + { + printf("%d\t", i); + } + } +} + +int main() +{ + int num; + printf("Enter a number to count till 100\n"); + scanf("%d", &num); + + natNum(num); + + return (0); +} diff --git a/TASKS/Task 014.c b/TASKS/Task 014.c index d139cd2..375c1e1 100644 --- a/TASKS/Task 014.c +++ b/TASKS/Task 014.c @@ -6,4 +6,24 @@ 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 + +void tenTimesNum() +{ + + for (int i = 0; i < 10; ++i)// why does < print up to 10 + { + for (int n = 0; n <= 14; ++n)// why does only < not print up to 14 + { + printf("%d ", n); + } + printf("\n"); + } + +int main() +{ + tenTimesNum(); +return(0); +} diff --git a/TASKS/Task 015.c b/TASKS/Task 015.c index 7cac93a..afd6f75 100644 --- a/TASKS/Task 015.c +++ b/TASKS/Task 015.c @@ -6,4 +6,32 @@ 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() +{ + for (int i = 1; i <= 100; ++i) + { + if (i % 15 == 0) + { + printf("FizzBuzz"); + } + + else if (i % 5 == 0) + { + printf("Buzz"); + } + else if (i % 3 == 0) + { + printf("Fizz"); + } + else + { + printf("%d", i); + } + printf("\n"); + } +return (0); +} diff --git a/TASKS/Task 016.c b/TASKS/Task 016.c index 8700bc0..7503746 100644 --- a/TASKS/Task 016.c +++ b/TASKS/Task 016.c @@ -8,4 +8,31 @@ Output: 720 // That is 1x2x3x4x5x6 = 720 -*/ \ No newline at end of file +*/ + +#include + +int recFunc(int n) +{ + + if (n == 1) + { + return n; + } + else + { + return n * recFunc(n-1); + + } + +} + +int main() +{ + int num, sum; + printf("Enter a number\n"); + scanf("%d", &num); + sum = recFunc(num), + + printf("%d", sum); +} diff --git a/TASKS/Task 017.c b/TASKS/Task 017.c index f5ef778..3d466e9 100644 --- a/TASKS/Task 017.c +++ b/TASKS/Task 017.c @@ -8,4 +8,31 @@ Output: 21 // That is; 1+2+3+4+5+6 = 21 -*/ \ No newline at end of file +*/ + +#include + +int recFunc(int n) +{ + + if (n == 1) + { + return n; + } + else + { + return n + recFunc(n-1); + + } + +} + +int main() +{ + int num, sum; + printf("Enter a number\n"); + scanf("%d", &num); + sum = recFunc(num), + + printf("%d", sum); +} diff --git a/TASKS/Task 018.c b/TASKS/Task 018.c index 147ff9a..8ba3b9b 100644 --- a/TASKS/Task 018.c +++ b/TASKS/Task 018.c @@ -2,4 +2,31 @@ 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 +*/ + +#include + +int power(int x, int y) +{ + int ans = 1; + while (y != 0) + { + ans *= x; + --y; + } + return (ans); +} + +int main() +{ + int n1, n2, result; + printf("Enter a base number: "); + scanf("%d", &n1); + printf("Enter an exponential: "); + scanf("%d", &n2); + + result = power(n1, n2); + printf("%d ^ %d = %d", n1, n2, result); + + return (0); +} diff --git a/TASKS/Task 019.c b/TASKS/Task 019.c index 5cc5874..69b9cf6 100644 --- a/TASKS/Task 019.c +++ b/TASKS/Task 019.c @@ -1,4 +1,45 @@ /* Write a function to convert temperature from Celsius to Fahrenheit and vice versa. -*/ \ No newline at end of file +*/ + +/* + Write a function to convert temperature from Celsius to + Fahrenheit and vice versa. +*/ +#include + + +float fah_cel(float a) +{ + float sum = (a - 32) * 5/9; + return sum; +} + +float cel_fah(float c) +{ + float sum; + sum = (c * 9/5) + 32; + return sum; +} + + +int main() +{ + float num, result; + printf("Enter the Fahrenheit number: "); + scanf("%f", &num); + result = fah_cel(num); + + float cel, result2, result3; + printf("Enter Celsius: "); + scanf("%f", &cel); + + result2 = cel_fah(cel); + + + printf("%.1f'F = %.1f'C\n", num, result); + printf("%.1f'C = %.1f'F\n", cel, result2); + + return (0); +} diff --git a/TASKS/Task-22.c b/TASKS/Task-22.c index 8cd01c4..1eae814 100644 --- a/TASKS/Task-22.c +++ b/TASKS/Task-22.c @@ -2,4 +2,15 @@ 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 age = 86; + int *ptr; + ptr = age; + printf("%d", age); + return (0); +} diff --git a/TASKS/Task-23.c b/TASKS/Task-23.c index dfddb16..2ae991e 100644 --- a/TASKS/Task-23.c +++ b/TASKS/Task-23.c @@ -1,4 +1,26 @@ /* Create a program that uses pointers to find the maximum value stored in an array. -*/ \ No newline at end of file +*/ + +#include + +int main() +{ + int nums[] = {95, 43, 6, 64, 23,89, 41, 15}; + int max; + int n = sizeof(nums) / sizeof(nums[0]); + + max = nums[3]; + + for (int i = 0; i < n; ++i) + { + if (nums[i] > max) + { + max = nums[i]; + } + + } + printf("the highest number is: %d\n", max); + return 0; +}