Posts

While and do while loop C

 int main() {     int x = 1;     while (x <= 10)       {     printf("%d\n", x);     x = x + 1;       }     return 0; } -------------------------------------  int main() {     int x = 1;     while (x <= 10)       {     printf("%d\n", x);     x++;       }     return 0; }     int x = 1;      do    {     printf("%d\n", x);     x++;   while (x <= 10);       }     return 0; }

guessing game using while and if

 #include <stdio.h> #include <stdlib.h> int main() {     int secreteNum = 5;     int guess;     int guessCount = 0;     int guessLimit = 3;     int outofGuesses = 0;         while (guess!=secreteNum && outofGuesses == 0){             if (guessCount < guessLimit){             printf("guess number: ");             scanf("%d", &guess);             guessCount++;         }         else{             outofGuesses = 1;         }         }         if (outofGuesses == 1){             printf("out of Guesses!");         }         else{     printf("You won!");     ...

Using Struct C

 struct Student { char name[50]; char major[50]; int age; double gpa; }; int main() { struct Student student1;     {     strcpy(student1.name, "Brad");     strcpy(student1.major, "Phornography");     student1.age = 18;     student1.gpa = 89.2;     };     printf("Name: %s\n", student1.name);     printf("Major: %s\n", student1.major);     printf("Age: %d\n", student1.age);     printf("GPA: %f\n", student1.gpa);     return 0; }

using function + if condition

 #include <stdio.h> #include <stdlib.h> //return on function int result; int max(int num1, int num2, int num3) {      if(num1>= num2 && num1>=num3){        result = num1;      }      else if(num2>= num1 && num2>=num3){        result = num2;      }     else {         result = num3;     }     return result; } int main() {     printf("max is %d\n", max(9,9,8));     return 0; }

using functions

 #include <stdio.h> #include <stdlib.h> int main() { sayHi("mike", 20); sayHi("john", 30); sayHi("doe", 40); return 0; } void sayHi(char name[], int age) {     printf("Say hi! %s, your age is %d\n", name, age); }

arrays in c programming

 #include <stdio.h> #include <stdlib.h> int main() {     int arraynumber[]={3,5,7,8,23,64,75,73};     //expects to output the index 7 which is 73. note that the index starts from 0 which is 3.     printf("%d", arraynumber[7]);     //printf("Hello world!\n");     return 0; }