Posts

Showing posts from March, 2022

for loop with index in C

  int main() {     int i;     int loopNumbr [] = { 2, 4, 5,65,7,8, 9}; //there are 6 indeces in these arrya start 0 index     for (i=0; i<7;i++){     printf("%d\n", loopNumbr[i] );     }     return 0; }

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; }

Mad Libs game in C programming

 #include <stdio.h> #include <stdlib.h> //making the mad libs game int main() {     char color[20], pluralNoun[20], celebrityNameF[20], celebrityNameL[20]; //Let the user input the field     printf("Enter Color:");     scanf("%s", color);     printf("Enter Plural Noun:");     scanf("%s", pluralNoun);     printf("Enter Celebrity Name:");     scanf("%s%s\n", celebrityNameF, celebrityNameL); //Print the output     printf("Roses are %s.\n", color);     printf("%s are blue.\n", pluralNoun);     printf("I love %s %s\n.", celebrityNameF, celebrityNameL);     return 0; }

basic addition of two decimal numbers

#include <stdio.h> #include <stdlib.h> int main() {     double num1, num2;     printf("enter numer:");     scanf("%lf", &num1);     printf("enter numer:");     scanf("%lf", &num2);     printf("sum is %f", num1 + num2);     return 0; }

when to use & ( & means ampersand) in scanf

use & in scanf when dealing with numbers;  example; int num1; printf("enter number:"); scanf("%d", &num1); char name[20]; printf("enter name:"); scanf("%s", name);

using the fgets instead of scanf

#include <stdio.h> #include<stdlib.h> int main() { char name[20];  printf("Enter your name:"); fgets(name, 20, stdin); printf("Your name is %s", name); return 0; }