diff --git a/00-introduction-to-c.md b/00-introduction-to-c.md index c3677b7..1d22c09 100644 --- a/00-introduction-to-c.md +++ b/00-introduction-to-c.md @@ -1,8 +1,15 @@ # Introduction To C What is C? + C is a compiled programming language. Who invented c? + C was invented by Denis Ritchie What year was c invented? + C was invented back in seventies Why are we learning to program in c? + What's special about c? + What is c used for? + Why did we start with C? + diff --git a/06-conditional-statements.c b/06-conditional-statements.c index b095295..90dbb47 100644 --- a/06-conditional-statements.c +++ b/06-conditional-statements.c @@ -1,3 +1,6 @@ +#include +int main (void) +{ /* C if Statement The syntax of the if statement in C programming is: @@ -17,6 +20,14 @@ Practice Program Check if a number is negative */ +int num; +printf("Check if your number is positive or negative\nEnter a number : "); +scanf("%d", &num); +if (num > 0) + printf("%d is positive\n", num); +else + printf("%d is negative\n", num); + /* C if...else Statement @@ -41,7 +52,13 @@ statements inside the body of if are skipped from execution. Practice Program Check whether a number is odd or even */ - +int num1; +printf("Check if a number is Even or Odd\nEnter a number : "); +scanf("%d", &num1); +if (num1 % 2 == 0) + printf("%d is Even\n", num1); +else + printf("%d is Odd\n", num1); /* C if...else Ladder The if...else statement executes two different codes depending @@ -70,4 +87,17 @@ else { Practice Program Program to relate two integers using ==, > or < symbol -*/ \ No newline at end of file +*/ +int age; +printf("Check your age\nEnter an age : "); +scanf("%d", &age); +if (age < 13) + printf("you are too young\n"); +else if (age >= 13 && age <= 18) + printf("You are a teenager\n"); +else + printf("you are an adult\n"); + + +return (0); +} diff --git a/07-loops.c b/07-loops.c index 72ea267..be5b4eb 100644 --- a/07-loops.c +++ b/07-loops.c @@ -1,3 +1,7 @@ +#include + +int main(void) +{ /* In programming, a loop is used to repeat a block of code until the specified condition is met. @@ -32,7 +36,10 @@ Practice Program Print Numbers 1 - 10 Calculate the sum */ - +printf("let's print numbers from 1 to 10 using for loop\n"); +for (int i = 1; i <= 10; i++) + printf("%d ",i); +putchar('\n'); /* C while loop The syntax of the while loop is: @@ -50,7 +57,14 @@ If testExpression is false, the loop terminates (ends). Practice Program Print Numbers 1 - 10 */ - +printf("let's print numbers from 1 to 10 using while loop\n"); +int i = 1; +while(i<=10) +{ + printf("%d ", i); + i++; +} +putchar('\n'); /* do...while loop The do..while loop is similar to the while loop with one important @@ -72,4 +86,13 @@ How do...while loop works? Practice Program Ask for correct input continously -*/ \ No newline at end of file +*/ +int num; +do{ + printf("Enter a random number, or 0 to quit : "); + scanf("%d", &num); + printf("You entered %d\n", num); +}while(num !=0); + +return (0); +} diff --git a/08-Ternary-Operator.c b/08-Ternary-Operator.c index 6d968b6..4db6ef2 100644 --- a/08-Ternary-Operator.c +++ b/08-Ternary-Operator.c @@ -16,4 +16,13 @@ Syntax condition ? value_if_true : value_if_false Ternary operator can also be nested -*/ \ No newline at end of file +*/ +#include +int main(void) +{ + int num; + printf("Enter an number : "); + scanf("%d",&num); + (num % 2 == 0) ? printf("%d is Even\n",num) : printf("%d is Odd\n", num); +return(0); +} diff --git a/09-Nested-Loops.c b/09-Nested-Loops.c index 2b01945..8dc4c41 100644 --- a/09-Nested-Loops.c +++ b/09-Nested-Loops.c @@ -1,3 +1,8 @@ +#include + +int main (void) +{ + /* Nested Loops A nested loop means a loop statement inside another @@ -33,7 +38,19 @@ while(condition) { Practice Problem FizzBuzz */ - +printf("FizzBuzz if divided by both 5 & 3.\nFizz is divided by 3.\nBuzz if divided by 5\n"); +for (int i = 1; i <= 100; i++) +{ + if ( i % 3 == 0 && i % 5 == 0) + printf("FizzBuzz "); + else if (i % 5 == 0) + printf("Buzz "); + else if (i % 3 == 0) + printf("Fizz "); + else + printf("%d ", i); +} +putchar('\n'); /* int n = 6;// variable declaration //printf("Enter the value of n :"); @@ -46,4 +63,7 @@ int n = 6;// variable declaration } printf("\n"); } -*/ \ No newline at end of file +*/ + +return (0); +} diff --git a/11-Switch-Statement.c b/11-Switch-Statement.c index 7015891..8d4fdf2 100644 --- a/11-Switch-Statement.c +++ b/11-Switch-Statement.c @@ -33,4 +33,48 @@ The default clause inside the switch statement is optional. Practice Program Create a calculator -*/ \ No newline at end of file +*/ +#include + +int main (void) +{ +printf("This is a basic calculator [a sign b]\nwhere 'a' & 'b' are numbers and 'sign' is one of those [+, -, *, /, %%]: \n"); +double a,b; +char sign; +printf("Enter number a : "); +scanf("%lf", &a); + +printf("Enter number b : "); +scanf("%lf", &b); + +printf("Enter the operaion you want to perform : "); +scanf(" %c", &sign); + +switch(sign) +{ + case '+': + printf("%.2lf %c %.2lf = %.2lf\n", a, sign, b, a + b); + break; + case '-': + printf("%.2lf %c %.2lf = %.2lf\n", a, sign, b, a - b); + break; + case '*': + printf("%.2lf %c %.2lf = %.2lf\n", a, sign, b, a * b); + break; + case '/': + printf("%.2lf %c %.2lf = %.2lf\n", a, sign, b, a / b); + break; + case '%': + printf("%.2lf %c %.2lf = %d\n", a, sign, b, (int) a % (int) b); + break; + + default : + printf("Error, please enter a correct operation\n"); +} + + + + +return (0); +} + diff --git a/TASKS/Task 001.c b/TASKS/Task 001.c index 24be041..a50fb01 100644 --- a/TASKS/Task 001.c +++ b/TASKS/Task 001.c @@ -4,4 +4,21 @@ 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) +{ +printf("Alpahbet letters in lowercase from a to z : \n"); +char c; +for (c = 97; c <= 122; c++) + printf("%c ",c); +putchar('\n'); + +printf("Alpahbet letters in lowercase from a to z exept e and q : \n"); +for (c = 97; c <= 122; c++) + if (c != 'e' && c != 'q') + printf("%c ",c); +putchar('\n'); + +return (0); +} diff --git a/TASKS/Task 002.c b/TASKS/Task 002.c index 5cdd390..f82c5b8 100644 --- a/TASKS/Task 002.c +++ b/TASKS/Task 002.c @@ -3,4 +3,23 @@ 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 (void) +{ + int score; + + printf("Enter your score : "); + scanf("%d", &score); + + if (score >= 0 && score < 80) + printf("your score is %d, wich is under 80\nYou are not eligible to enroll to the program.\n", score); + else if (score >= 80 && score <=100) + printf("your score is %d\nYou can enroll to the program\n", score); + else if (score < 0 || score > 100) + printf("Error... please enter a valid score\n"); + + return (0); +} diff --git a/TASKS/Task 003.c b/TASKS/Task 003.c index 7fca79c..46b4385 100644 --- a/TASKS/Task 003.c +++ b/TASKS/Task 003.c @@ -6,4 +6,22 @@ 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 (void) +{ + int num; + + printf("Enter a number : "); + scanf("%d", &num); + + if (num == 0) + printf("%d is Zero\n", num); + else if (num % 2 == 0) + printf("%d is Even\n", num); + else + printf("%d is Odd\n", num); + + return (0); +} diff --git a/TASKS/Task 004.c b/TASKS/Task 004.c index 17ee38c..e506c9c 100644 --- a/TASKS/Task 004.c +++ b/TASKS/Task 004.c @@ -11,4 +11,20 @@ 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 (void) +{ + char name[100]; + int age; + + printf("Enter your name : "); + scanf("%s", name); + printf("Enter your age : "); + scanf("%d", &age); + + printf("Your name is %s, and you are %d years old.\n", name, age); +return (0); +}