Posts

Showing posts from August, 2020

Star

  #include<stdlib.h> #include<graphics.h> #include<conio.h> #include<dos.h> int main(){ int gd = DETECT,gm,i,x,y; initgraph(&gd,&gm,"c:\\tc\\bgi"); while(!kbhit()){ for (i = 0;i<=500;i++){ x = rand() %getmaxx(); y = rand() %getmaxy(); putpixel(x,y,15); } delay(1000); cleardevice(); } getch(); closegraph(); }

Rainbow

  #include<stdlib.h> #include<graphics.h> #include<conio.h> #include<dos.h> int main(){ int gd = DETECT,gm,i,x,y; initgraph(&gd,&gm,"c:\\tc\\bgi"); for (i = 30;i<=200;i++){ delay(100); setcolor(i/10); arc(getmaxx()/2,getmaxy()/2,0,180,i); } getch(); closegraph(); }

Rocket game

  #include<stdio.h> #include<conio.h> #include<graphics.h> #include<time.h> int l = 10,b = 60,x=4,y=4,gameend = 0; int ex = 50,ey= 8,r1,ex2 = 59,ey2= 5,r2; int ex3 = 40,ey3 = 2,r3,score = 0; void home(); void control(); void draw(); int main(){ home(); while(gameend != 1){ clrscr(); draw(); control(); delay(200); } clrscr(); printf("score = %d ",score); getch(); return 0; } void draw(){ int i,k; for(i=0;i<l;i++){ for(k=0;k<b;k++){ if(i==0||k==0||i==9||i==6||i==3){ printf("*"); } else if(i==y&&k==x||i==y+1&&k==x){ printf(")"); } else if(i==y&&k==x+1||i==y+1&&k==x+1){ printf("0"); } else if(i ==y&&k==x+2){ printf("0"); } else if(i ==y+1&&k==x+2){ printf("0"); } else if(i ==y&&k==x+3){ printf("0"); } else if(i ==y+1&&k==x+3){ printf("0"); } else if(i == ey&&k==ex){ printf(...

bird game

  #include<stdio.h> #include<conio.h> #include<dos.h> #include<time.h> int x=5,y=2,gameend =0,w1,e,upl=2,upb=50,ch = 0 ,upl2 = 2; int upb2 = 70,downl1 = 9,downb1 = 60,downb2 = 77,downl2 = 9; int upl3= 2,upb3 =35 ,downl3 =9 ,downb3 = 50,score = 0; void draw(); void move(); int main(){ while(gameend != 1) { clrscr(); draw(); move(); score++; delay(300); } clrscr(); printf("score = %d",score); getch(); return 0; } void draw() { int l,b; for (l=0;l<10;l++) { for(b=0;b<78;b++){ if (l==0||l==9||b==0){ printf("-"); } else if ( ch == 15){ ch =0; } else if(l==x&&b==y){ printf("0"); } else if(upb-1 == b&&upl-1 == l){ printf("|"); upb--; } else if(upb == b&&upl == l){ printf("|"); } else if(upb2-1 == b&&upl2-1 == l){ printf("|"); upb2--; } else if(upb2 == b&&upl2 == l){ printf("|...

Tic tac tic game

  #include<stdio.h> #include<conio.h> int gameover=1; char a[9] = {'1','2','3','4','5','6','7','8','9'}; char p1name[20],p2name[20]; char symbol1='x',symbol2 = '0'; int player1,player2,ch = 0; void input(); void show(); void win(); int main(){ clrscr(); printf("enter player 1 name : "); scanf("%s",p1name); printf("enter player 2 name : "); scanf("%s",p2name); while(gameover!= 0){ clrscr(); show(); input(); win(); } clrscr(); show(); win(); getch(); return 0; } void show(){ printf("\t\t\t\t TIC TAC TIC\n"); printf("\n\n"); printf("\t\t\t--|--|--\n"); printf("\t\t\t%c |%c |%c \n",a[0],a[1],a[2]); printf("\t\t\t--|--|--\n"); printf("\t\t\t%c |%c |%c \n",a[3],a[4],a[5]); printf("\t\t\t--|--|--\n"); printf("\t\t\t%c |%c |%c \n",a[6],a[7],a[8]); print...

Printing 7table using malloc and realloc

 #include<stdio.h> #include<stdlib.h> int main(){     int *ptr;     ptr = (int*) malloc(10*sizeof(int));     for(int i=0; i<10;i++){         ptr[i] = 7*(i+1);           printf("The value of 7 X %d = %d \n", i+1,ptr[i]);      }       ptr = realloc(ptr, 15*sizeof(int));     printf("\nAfter reallocating.....\n\n");     for(int i=0; i<15;i++){         ptr[i] = 7*(i+1);           printf("The value of 7 X %d = %d \n", i+1,ptr[i]);      }          return 0; }

Using malloc () and realloc ()

 #include<stdio.h> #include<stdlib.h> int main(){     int *ptr;     ptr = (int*) malloc(5*sizeof(int));     for(int i=0; i<5;i++){         printf("Enter the value of %d element: \n", i);         scanf("%d", &ptr[i]);     }     for(int i=0; i<5;i++){         printf("The value of %d element is: %d \n", i, ptr[i]);      }     ptr = realloc(ptr, 10*sizeof(int));     for(int i=0; i<10;i++){         printf("Enter the value of %d element: \n", i);         scanf("%d", &ptr[i]);     }     for(int i=0; i<10;i++){         printf("The value of %d element is: %d \n", i, ptr[i]);      }     return 0; }

Using realloc()

 #include<stdio.h>  #include<stdlib.h>  int main(){     int *ptr;      ptr = (int *) malloc(6 * sizeof(int));      for(int i=0; i<6;i++){         printf("Enter the value of %d element: \n", i);          scanf("%d", &ptr[i]);     }     for(int i=0; i<6;i++){         printf("The value of %d element is: %d\n", i, ptr[i]);      }     // Reallocate ptr using realloc()     ptr = realloc(ptr, 10*sizeof(int));          for(int i=0; i<10;i++){         printf("Enter the value of %d element: \n", i);          scanf("%d", &ptr[i]);     }     for(int i=0; i<10;i++){         printf("The value of %d element is: %d\n", i, ptr[i]);      }     return 0; }

Using free()

 #include<stdio.h>  #include<stdlib.h>  int main(){     int *ptr;     int *ptr2;     // Sizeof operator in C     // printf("The size of int on my pc is %d\n", sizeof(int));     // printf("The size of float on my pc is %d\n", sizeof(float));     // printf("The size of char on my pc is %d\n", sizeof(char));     ptr = (int *) malloc(600 * sizeof(int));      for(int i=0; i<600;i++){         ptr2 = (int *) malloc(600000 * sizeof(int));          printf("Enter the value of %d element: \n", i);          scanf("%d", &ptr[i]);         free(ptr2);     }     for(int i=0; i<6;i++){         printf("The value of %d element is: %d\n", i, ptr[i]);      }     return 0; }

Using calloc()

#include<stdio.h>  #include<stdlib.h>  int main(){     int *ptr;      int n;     printf("How many integers do you want to enter:\n");     scanf("%d", &n);     ptr = (int *) calloc(n, sizeof(int));      for(int i=0; i<n;i++){         printf("Enter the value of %d element: \n", i);          scanf("%d", &ptr[i]);     }     for(int i=0; i<n;i++){         printf("The value of %d element is: %d\n", i, ptr[i]);      }     return 0; }

Using malloc()

 #include<stdio.h>  #include<stdlib.h>  int main(){     int *ptr;     // Sizeof operator in C     // printf("The size of int on my pc is %d\n", sizeof(int));     // printf("The size of float on my pc is %d\n", sizeof(float));     // printf("The size of char on my pc is %d\n", sizeof(char));     ptr = (int *) malloc(6 * sizeof(int));      for(int i=0; i<6;i++){         printf("Enter the value of %d element: \n", i);          scanf("%d", &ptr[i]);     }     for(int i=0; i<6;i++){         printf("The value of %d element is: %d\n", i, ptr[i]);      }     return 0; }

Printing 1 file to another file with double print of character

 #include<stdio.h> int main(){     FILE *ptr1;     FILE *ptr2;      ptr1 = fopen("a.txt", "r");     ptr2 = fopen("b.txt", "w");     char c = fgetc(ptr1);     while(c!=EOF){         fputc(c, ptr2);         fputc(c, ptr2);         c = fgetc(ptr1);     }     fclose(ptr1);      fclose(ptr2);      return 0; }

multiplication in file

 #include<stdio.h> int main(){     FILE *ptr;     int num;     printf("Enter the integer you need the table of\n");     scanf("%d", &num);     ptr = fopen("table.txt", "w");     for(int i=0; i<10; i++){         fprintf(ptr, "%d X %d = %d\n", num, i+1, num*(i+1));     }     fclose(ptr);     printf("Successfully generated table of %d to table.txt\n", num);     return 0; }

File read using fgetc ()

 #include<stdio.h> int main(){     FILE *ptr;     char c;     ptr = fopen("getcdemo.txt", "r");     c = fgetc(ptr);     while(c!=EOF){         printf("%c", c);         c = fgetc(ptr);     }     return 0; }

Using fgetc() and fputc()

 #include<stdio.h> int main(){     FILE *ptr;     // fgetc demo for reading a file     // ptr = fopen("getcdemo.txt", "r");     // char c = fgetc(ptr);     // printf("The value of my character is %c\n", fgetc(ptr));     // printf("The value of my character is %c\n", fgetc(ptr));     // printf("The value of my character is %c\n", fgetc(ptr));     // printf("The value of my character is %c\n", fgetc(ptr));     // printf("The value of my character is %c\n", fgetc(ptr));     ptr = fopen("putcdemo.txt", "w");     putc('c', ptr);     putc('c', ptr);     putc('c', ptr);     fclose(ptr);     return 0; }

File writing

 #include<stdio.h> int main(){     FILE *fptr;     int number = 45;     fptr = fopen("generated.txt", "w");     fprintf(fptr, "The number is %d\n", number);     fprintf(fptr, "Thanks for using fprintf", number);     fclose(fptr);     return 0; }

If file doesn't exit it return NULL

 #include<stdio.h> int main(){     FILE *ptr;     int num;     int num2;     ptr = fopen("sunil.txt", "r");     if (ptr == NULL){         printf("This file does not exist\n");     }     else{          fscanf(ptr, "%d", &num);         fscanf(ptr, "%d", &num2);         fclose(ptr);         printf("The value of num is %d\n", num);         printf("The value of num2 is %d\n", num2);     }     return 0; }

Reading files and initialise file

 #include<stdio.h> int main(){     FILE *ptr;     int num;     int num2;     ptr = fopen("sunil.txt", "r");//for reading file     fscanf(ptr, "%d", &num);     fscanf(ptr, "%d", &num2);     fclose(ptr);     printf("The value of num is %d\n", num);     printf("The value of num2 is %d\n", num2);     return 0; }

Compare dates

 #include<stdio.h> typedef struct date{     int date;     int month;     int year; }date; void display(date d){     printf("The date is: %d/%d/%d\n", d.date, d.month, d.year); } int dateCmp(date d1, date d2){     // Make decision on the basis of Year comparison     if(d1.year>d2.year){         return 1;     }     if(d1.year<d2.year){         return -1;     }     // Make decision on the basis of Month comparison     if(d1.month>d2.month){         return 1;     }     if(d1.month<d2.month) {         return -1;     }     // Make decision on the basis of Date comparison     if(d1.date>d2.date){         return 1;     }     if(d1.date<d2.date) {         return -1;  ...

Real and complex number with structure

 #include<stdio.h> typedef struct complex{     int real;     int complex; }comp; void display(comp c){     printf("The value of real part is %d\n", c.real);     printf("The value of Imaginary part is %d\n", c.complex); } int main(){     comp cnums[5];     for(int i=0; i<5; i++){         printf("Enter the real value for %d num\n", i+1);         scanf("%d", &cnums[i].real);         printf("Enter the complex value for %d num\n", i+1);         scanf("%d", &cnums[i].complex);     }     for(int i=0; i<5; i++){         display(cnums[i]);     }     return 0; }

Using typedef

 #include<stdio.h> #include<string.h> typedef struct employee{     int code;     float salary;     char name[20]; } emp; void show(emp emp1){     printf("The Code of employee is: %d\n", emp1.code);     printf("The Salary of employee is: %f\n", emp1.salary);     printf("The Name of employee is: %s\n", emp1.name);      } int main(){     // Declaring e1 and ptr     emp e1;     emp *ptr;     // pointing ptr to e1     ptr = &e1;      // Set the member values for e1     ptr->code = 101;     ptr->salary = 11.01;     strcpy(ptr->name, "Harry");      show(e1);     return 0; }

Structure with function

 #include<stdio.h> #include<string.h> struct employee{     int code;     float salary;     char name[20]; }; void show(struct employee emp){     printf("The Code of employee is: %d\n", emp.code);     printf("The Salary of employee is: %f\n", emp.salary);     printf("The Name of employee is: %s\n", emp.name);     emp.code = 34; } int main(){     struct employee e1;     struct employee *ptr;     ptr = &e1;     //(*ptr).code = 101; //or you can also write: ptr->code = 101;     ptr->code = 101;     ptr->salary = 11.01;     strcpy(ptr->name, "Harry");     show(e1);      printf("The Code of employee is: %d\n", e1.code);     return 0; }

Using Pointer to structure

 #include<stdio.h> #include<string.h> struct employee{     int code;     float salary;     char name[20]; }; int main(){     struct employee e1;     struct employee *ptr;     ptr = &e1;     //(*ptr).code = 101; //or you can also write: ptr->code = 101;     ptr->code = 101;     printf("%d", e1.code);      return 0; }

Another way to initialise structure

 #include<stdio.h> #include<string.h> struct employee{     int code;     float salary;     char name[20]; }; int main(){     struct employee sunil= {100, 34.23, "sunil"};     printf("Code is: %d \n", sunil.code);     printf("Salary is: %f \n", sunil.salary);     printf("Name is: %s \n", sunil.name);       return 0; }

Using array + structure

 #include<stdio.h> #include<string.h> struct employee{     int code;     float salary;     char name[20]; }; int main(){     struct employee facebook[100];     facebook[0].code = 100;     facebook[0].salary = 100.45;     strcpy(facebook[0].name, "sunil");     facebook[1].code = 101;     facebook[1].salary = 90.45;     strcpy(facebook[1].name, "Bhaskar");     facebook[2].code = 102;     facebook[2].salary = 110.45;     strcpy(facebook[2].name, "SkillKhiladi");     printf("Done");     return 0; }

Structure input

 #include<stdio.h> #include<string.h> struct employee{     int code;     float salary;     char name[10]; }; int main(){     struct employee e1, e2, e3;     printf("Enter the value for code of e1: ");     scanf("%d", &e1.code);     printf("Enter the value for salary of e1: ");     scanf("%f", &e1.salary);     printf("Enter the value for name of e1: ");     scanf("%s", e1.name);     printf("Enter the value for code of e2: ");     scanf("%d", &e2.code);     printf("Enter the value for salary of e2: ");     scanf("%f", &e2.salary);     printf("Enter the value for name of e2: ");     scanf("%s", e2.name);     printf("Enter the value for code of e3: ");     scanf("%d", &e3.code);     printf("Enter the value for salary of e3: ");     scanf("%f", &e3.salary...

Structure basic

 #include<stdio.h> #include<string.h> struct employee{     int code;     float salary;     char name[10]; }; int main(){     int a =34;     char b = 'g';     float d = 234.3543;     // employee e1;     // e1.salary = 34.454; --->wont work without employee structure     struct employee e1;     e1.code = 100;     e1.salary = 34.454;     // e1.name = "sunil"; --> wont work     strcpy(e1.name, "sunil");     printf("%d\n", e1.code);     printf("%.3f\n", e1.salary);     printf("%s\n", e1.name);     return 0; }

Making occurrence function (or) finding how many words are there by a character (or) how many times a given character occur

 #include<stdio.h> int occurence(char st[], char c){     char *ptr = st;     int count=0;     while(*ptr!='\0'){         if (*ptr==c){             count++;         }         ptr++;     }     return count; } int main(){     char st[] = "sunil777";     int count = occurence(st, '7');     printf("Occurences = %d", count);     return 0; }

Making decrypt function

 #include<stdio.h> void decrypt(char *c){     char *ptr = c;     while(*ptr!='\0'){         *ptr = *ptr - 1;          ptr++;     } } int main(){     char c[] = "Tvojm!Lvnbs";     decrypt(c);     printf("Decrypted string is: %s", c);     return 0; }

Making Encrypt function

 #include<stdio.h> void encrypt(char *c){     char *ptr = c;     while(*ptr!='\0'){         *ptr = *ptr + 1;          ptr++;     } } int main(){     char c[] = "Sunil Kumar";     encrypt(c);     printf("Encrypted string is: %s", c);     return 0; }

Printing from to (or) making slice function

 #include<stdio.h> void slice(char *st, int m, int n){     int i = 0;      while((m+i)<n){          st[i] = st[i+m];          i++;      }      st[i] = '\0'; } int main(){     char st[] = "sunilkumartdgrfg";     slice(st, 1, 6);     printf("%s", st);     return 0; }

Making strlen function

 #include<stdio.h> int strlen(char * st){     char *ptr = st;     int len=0;     while(*ptr!='\0'){         len++;         ptr++;     }     return len; } int main(){     char st[] = "sunil";     int l = strlen(st);     printf("The length of this string is %d", l);     return 0; }

Using fflush

 #include<stdio.h> #include<string.h> int main(){     char st1[34];     char st2[34];     char c;     int i =0;     printf("Enter the value of first string\n");     scanf("%s", st1);      printf("Enter the value of second string character by character\n");          while(c!='\n'){          fflush(stdin);         scanf("%c", &c);          st2[i] = c;         i++;     }     st2[i-1]= '\0';     printf("The value of st1 is %s\n", st1);     printf("The value of st2 is %s\n", st2);     printf("strcmp for these strings returns %d", strcmp(st1, st2));     return 0; }

Using strcmp

 #include<stdio.h> #include<string.h> int main(){     char st1[45] = "Hel";     char *st2 = "Helo";     int val = strcmp(st1, st2);     printf("Now the val is %d", val);     return 0; }

Using strcat

#include<stdio.h> #include <string.h> int main(){     char st1[45] = "Hello";     char *st2 = "sunil";     strcat(st1, st2);     printf("Now the st1 is %s", st1);     return 0; }

Using strcpy

 #include<stdio.h> #include <string.h> int main(){     char *st = "This";     char st2[45];     strcpy(st2, st);     printf("Now the st2 is %s", st2);     return 0; }

Using strlen

 #include<stdio.h> #include <string.h> int main(){     char *st = "sunil";     int a = strlen(st);     printf("The length of string st is %d", a);     return 0; }

Different method to initialise string

 #include<stdio.h> int main(){      char *ptr = "Sunil Bhai";     // char ptr[] = "Sunil ; // We can change string with *ptr     ptr = "Bhaskar";     printf("%s", ptr);     return 0; }

Using gets() and puts()

 #include<stdio.h> int main(){     char s[34];     printf("Enter your name: ");     gets(s);      puts(s);      // printf("Your name is %s", s);     return 0; }

Inputting string

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

Convenit way of printing string

 #include<stdio.h> int main(){     // int a = 4;     // printf("%d", a);     // char *ptr = "sunil bhai";     char ptr[] = "sunilkumar";     printf("%s", ptr);     return 0; }

Print string character by character

 #include<stdio.h> int main(){     // char str[] = "sunil";     char str[] = {'s', 'u', 'n', 'i', 'l', '\0'};     char *ptr = str;     while(*ptr!='\0'){         printf("%c", *ptr);         ptr++;     }     return 0; }

3d array address

 #include<stdio.h> int main(){     int arr[2][3][4];     for(int i=0;i<2;i++){         for(int j=0;j<3;j++){             for(int k=0;k<4;k++){                 printf("The address of arr[%d][%d][%d] is %u\n", i, j, k, &arr[i][j][k]);             }         }     }          return 0; }

Multiplication with function

 #include<stdio.h> void printTable(int *mulTable, int num, int n){     printf("The multiplication table of %d is :\n", num);     for(int i=0; i<n; i++){         mulTable[i] = num*(i+1);     }     for(int i=0; i<n; i++){         printf("%dX%d = %d\n", num, i+1, mulTable[i]);     }     printf("******************************************************\n\n"); } int main(){     int mulTable[3][10];     printTable(mulTable[0], 2, 10);     printTable(mulTable[1], 7, 10);     printTable(mulTable[2], 9, 10);     return 0; }

Reverse array

 #include<stdio.h> void reverse(int *arr, int n){     int temp;     for(int i=0; i<(n/2); i++){         temp = arr[i];         arr[i] = arr[n-i-1];         arr[n-i-1] = temp;     } } int main(){     int arr[] = {1,2,3,4,5,6,7};     reverse(arr, 7);     for(int i=0; i<7; i++){          printf("The value of %d element is: %d\n", i, arr[i]);     }     return 0; }

Array with pointer

 #include<stdio.h> int main(){     int arr[10];     // int *ptr = &arr[0];     int *ptr = arr;     ptr = ptr+2;     if(ptr==&arr[2]){         printf("These point to the same location in memory\n");     }     else{         printf("These do not point to the same location in memory\n");     }     return 0; }

N students and their Marks with array

 #include<stdio.h> int main(){     int n_students = 3;     int n_subjects = 5;     int marks[3][5];     for(int i=0; i<n_students; i++){         for(int j=0; j<n_subjects; j++){             printf("Enter the marks of student %d in subject %d\n", i+1, j+1);             scanf("%d", &marks[i][j]);         }     }     for(int i=0; i<n_students; i++){         for(int j=0; j<n_subjects; j++){             printf("The marks of student %d in subject %d is: %d\n", i+1, j+1, marks[i][j]);         }     }     return 0; }

Array with function

 #include<stdio.h> // void printArray(int *ptr, int n){ //     for(int i=0; i<n; i++){ //         printf("The value of element %d is %d\n", i+1, *(ptr+i));  //     } // } void printArray(int ptr[], int n){     for(int i=0; i<n; i++){         printf("The value of element %d is %d\n", i+1, ptr[i]);      }      ptr[2] = 5555; // This value will be changes in arr array of main as well } int main(){     int arr[] = {1,2,3543,34,3,645,23};     printArray(arr, 7);     printf("%d", arr[2]);     return 0; }

Array with pointer

 #include<stdio.h> int main(){     int marks[4];     int *ptr;     // ptr = &marks[0];     ptr = marks;     for(int i =0; i<4; i++){         printf("Enter the value of marks for student %d: \n", i+1);         scanf("%d", ptr);         ptr ++;     }     for(int i =0; i<4; i++){         printf("The value of marks for student %d is %d \n",i+1, marks[i]);       }     return 0; }

Pointer arithmetic

 #include<stdio.h> int main(){     // int i = 34;     // int *ptr = &i;     // printf("The value of ptr is %u\n", ptr);     // // ptr = ptr - 1;     // ptr = ptr + 1;     // // ptr++;     // // ptr++;     // printf("The value of ptr is %u\n", ptr);     // char c = '3';     // char *ptr = &c;     // printf("The value of ptr is %u\n", ptr);     // // ptr = ptr - 1;     // ptr = ptr + 1;     // ptr = ptr + 1;     // // ptr++;     // // ptr++;     // printf("The value of ptr is %u\n", ptr);     float f = 3.4;     float *ptr = &f;     printf("The value of ptr is %u\n", ptr);     // ptr = ptr - 1;     ptr = ptr + 1;     // ptr = ptr + 1;     // ptr++;     // ptr++;     printf("The value of ptr is %u\n", ptr); ...

Array basis

#include<stdio.h> int main(){     int marks[5];     for(int i=0; i<5; i++)     {         printf("Enter the value of marks for student %d: ", i+1);         scanf("%d", &marks[i]);     }     for(int i=0; i<5; i++)     {         printf("The value of marks for student %d is: %d \n", i+1, marks[i]);      }     return 0; }

Sum and average with call by reference

 #include<stdio.h> void sumAndAvg(int a, int b, int *sum, float *avg){     *sum = a +b;     *avg = (float)(*sum)/2; } int main(){     int i, j, sum;     float avg;     i = 3;     j = 6;     sumAndAvg(i, j, &sum, &avg);     printf("The value of sum is %d \n", sum);     printf("The value of avg is %f \n", avg);          return 0; }

Address of variable

 #include<stdio.h> int main(){     int a=6;     int *ptr;     ptr = &a;     printf("The value of variable a is %d\n", a);     printf("The address of variable a is %u\n", ptr);     printf("The value of variable a is %d\n", *ptr);     return 0; }

Swap with call by reference

 #include<stdio.h> void wrong_swap(int a, int b); void swap(int *a, int *b); int main(){     int x=3, y=4;     printf("The value of x and y before swap is %d and %d\n", x, y);     //wrong_swap(x, y); // will not work due to call by value     swap(&x, &y); // will work due to call by reference     printf("The value of x and y after swap is %d and %d\n", x, y);     return 0; } void wrong_swap(int a, int b){     int temp;     temp = a;     a = b;     b = temp; } void swap(int *a, int *b){     int temp;     temp = *a;     *a = *b;     *b = temp; }

Sum with call by value

 #include<stdio.h> int sum (int a, int b); int main(){     int x=4, y=7;     printf("The value of x and y is %d and %d\n", x, y);     printf("The value of 4+7 is %d\n", sum(x, y));     printf("The value of x and y after function call is %d and %d\n", x, y);     return 0; } int sum (int a, int b){     int c;     c = a + b;     b =3434;//doesn't change in main()     a = 23432;       return c; }

Pointers basis

 #include<stdio.h> int main(){     int i = 34;     int *j = &i; // j will now store the address of i     printf("The value of i is %d\n", i);     printf("The value of i is %d\n", *j);     printf("The address of i is %u\n", &i);     printf("The address of i is %u\n", j);     printf("The address of j is %u\n", &j);     printf("The value of j is %u\n", *(&j));     return 0; }

Print pattern

 #include<stdio.h> void printPattern(int n); int main(){     int n =4;     printPattern(n);     return 0; } // for n = 3 // * // *** // ***** // 1 - 1 // 2 - 3 // 3 - 5 // (2n-1) void printPattern(int n){     if (n==1){         printf("*\n");         return;     }     printPattern(n-1);     for(int i=0;i<(2*n-1);i++){         printf("*");     }     printf("\n"); }

Tricky with increment

 #include<stdio.h> int main(){     int a =3;     printf("%d %d %d", a, ++a, a++);     return 0; }

Find force

 #include<stdio.h> float force(float mass); int main(){     float m;     printf("Enter the value of mass in kgs\n");     scanf("%f", &m);     printf("The value of force in Newton is %.2f\n", force(m));      return 0; } float force(float mass){     float result = mass * 9.8;     return result; }

Find average with function

 #include<stdio.h> float average(int a, int b, int c); int main(){     int a, b, c;     printf("Enter the value of a\n");     scanf("%d", &a);     printf("Enter the value of b\n");     scanf("%d", &b);     printf("Enter the value of c\n");     scanf("%d", &c);     printf("The value of average is %f", average(a, b, c));     return 0; } float average(int a, int b, int c){     float result;     result = (float)(a + b + c)/3;     return result; }

Factorial with recursion

 #include<stdio.h> int factorial(int x); int main(){     int a = 5;     printf("The value of factorial %d is %d", a, factorial(a));     return 0; } int factorial(int x){     printf("Calling factorial(%d)\n", x);     if (x==1 || x==0){         return 1;     }     else{         return x * factorial(x-1);     } }

Area of square

 #include<stdio.h> #include<math.h> int main(){     int side;     printf("Enter the value of side\n");     scanf("%d", &side);     printf("The value of area is %f", pow(side,2));     return 0; }

Sum of two numbers with function

 #include<stdio.h> // sum is a function which takes a and b as input and returns an integer as an output int sum(int a, int b); // function prototype declaration int main(){     int c;     c = sum(2, 15); // function call     printf("The value of c is %d\n", c);     return 0; } int sum (int a, int b){     int c;     c = a + b;     return c; }

Functions in function

 #include<stdio.h> void goodMorning(); void goodAfternoon(); void goodNight(); int main(){     goodMorning();     return 0; } void goodMorning(){     printf("Good Morning Harry\n");     goodAfternoon(); } void goodAfternoon(){     printf("Good Afternoon Harry\n");     goodNight(); } void goodNight(){     printf("Good Night Harry\n"); }

Check prime number

 #include<stdio.h> int main(){     // Prime Numbers = A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers.      // Disclaimer: This is not the best method to solve this problem     int n = 2, prime=1;     for(int i=2;i<n;i++){         if (n%i==0 ){             prime = 0;             break;         }     }     if (prime==0){         printf("This is not a prime number");     }     else{         printf("This is a prime number");     }     return 0; }

Find factorial

 #include<stdio.h> int main(){     // factorial(4) - 1 * 2 * 3 * 4      // factorial(6) - 1 * 2 * 3 * 4 * 5 * 6     int i=0, n=7, factorial=1;     for(i=1;i<=n;i++){         factorial *=i;     }     printf("The value of factorial %d is %d", n, factorial);     return 0; }

Sum of Frist n number s

 #include<stdio.h> int main(){     int i=1, sum=0, n=10;     // for(i=1; i<=n; i++){     //     sum +=i;     // }     while( i<=n){         sum +=i;         i++;      }     printf("The value of sum(1 to 10) is %d", sum);     return 0; }

Using continue function

 #include<stdio.h> int main(){     int skip=5, i=0;     while(i<10){         i++;         if(i!=skip){             continue;         }         else{             printf("%d\n", i);         }     }     return 0; }

Using break function

 #include<stdio.h> int main(){     int i = 0;     do{         printf("The value of i is %d\n", i);         if(i==4){             break;         }         i++;     }while(i<10);     return 0; }

Using for loop

 #include<stdio.h> int main(){     int n;     printf("Enter the value of n \n");     scanf("%d", &n);     for(int i=n; i ; i--){         printf("The value of i is %d\n", i);     }     return 0; }

Using do while loop

 #include<stdio.h> int main(){     int i = 220;     do{         printf("The value of i is %d\n", i);         i++;     }while(i <10);     return 0; }

Increment operater

 #include<stdio.h> int main(){     int i = 5;     printf("The value after i++ is %d\n", ++i);     i++; // ---> Pehle print fir increment     ++i; // ---> Pehle increment fir print kare     printf("The value of i is %d\n", i);     i+=10; //--> Increments i by 10     printf("The value of i is %d\n", i);     return 0; }

Using while loops

 #include<stdio.h> int main(){     int a;     scanf("%d", &a);     while(a<10){     //     a = 11;     // while(a>10){ ---> These two lines will lead to an infinite loop         printf("%d\n", a);         a++;     }     return 0; }

Using If ,else in one line

 #include<stdio.h> int main(){     int a;     printf("Enter a\n");     scanf("%d", &a);     // One liner     (a < 5) ? printf("A is less than 5") : printf("A is not less than 5");      return 0; }

Using switch statement

 #include<stdio.h> int main(){     int rating;     printf("Enter your rating (1-5)\n");     scanf("%d", &rating);     switch(rating){         case 1:             printf("Your rating is 1\n");             break;         case 2:             printf("Your rating is 2\n");             break;         case 3:             printf("Your rating is 3\n");             break;         case 4:             printf("Your rating is 4\n");             break;         case 5:             printf("Your rating is 5\n");             break;        ...

Student marks enter

 #include<stdio.h> int main(){     int physics, chemistry, maths;     float total;     printf("Enter Physics Marks\n");     scanf("%d", &physics);     printf("Enter Chemistry Marks\n");     scanf("%d", &chemistry);     printf("Enter Maths Marks\n");     scanf("%d", &maths);     total = (physics + maths + chemistry)/3;     if((total<40) || physics<33 || maths<33 || chemistry<33){         printf("Your total percentage is %d and you are fail\n", total);     }     else{         printf("Your total percentage is %d and you are pass\n", total);     }     return 0; }

Check lower case

 #include<stdio.h> int main(){     // 97-122 = a-z ASCII Values     char ch;     printf("Enter the character\n");     scanf("%c", &ch);     if(ch<=122 && ch>=97){         printf("It is lowercase");     }     else{         printf("It is not lowercase");     }     return 0; }

Income tax finding

 #include<stdio.h> int main(){     float tax = 0, income;     printf("Enter your income\n");     scanf("%f", &income);     if(income>=250000 && income<=500000){         tax = tax + 0.05 * (income - 250000);     }          if (income >= 500000 && income <= 1000000)     {         tax = tax + 0.20 * (income - 500000);     }     if (income >= 1000000)     {         tax = tax + 0.30 * (income - 1000000);     }          printf("Your net income tax to be paid by 26th of this month is %f\n", tax);     return 0; }

Using if ,else if ,else and logical operaters

#include<stdio.h> int main(){     int age;     int vipPass = 0;     // vipPass = 1;     printf("Enter your age\n");     scanf("%d", &age);      // if (age <= 70 && age>=18)     if ((age <= 70 && age>=18) || (vipPass==1))     {         printf("You are above 18 and below 70, you can drive\n");     }     else     {         printf("You cannot drive\n");     }         return 0; }

Type declaration

 #include<stdio.h> int main(){     // int a = 4; // Type declaration instruction     // int a = 4, b, c; // Type declaration instruction     // b = c = a;     // printf("The value of a is %d\n", a);     // printf("The value of b is %d\n", b);     // printf("The value of c is %d\n", c);     float a = 1.1;     float b = a + 8.9;     printf("The value of b is %f\n", b);     return 0; }

Division check

 #include<stdio.h> int main(){     // int a; int b=a;     // int v = 3^3;     // char dt = '2';     // float d = (3.0/8-2);     // printf("%d\n", v);     // printf("%f\n", d);     // Q3. Write a program to determine whether a number is divisible by 97 or not     int num;     printf("Enter the number\n");     scanf("%d", &num);     printf("Divisibility test returns: %d\n", num%97);     // Q4. Step by step evaluation of 3*x/y-z+k     int x = 2, y=3, z=3, k=1;     int result = 3 * x / y - z + k;     // 6/3 - 3 + 1     // 2 - 3 + 1     // 2 - 3 + 1      // 0     printf("The value of result is %d", result);         return 0; }

Operater presidency

 #include <stdio.h> int main() {     int x = 2;     int y = 3;     printf("The value of 3*x - 8*y is %d \n", 3*x - 8*y);     printf("The value of 8*y / 3*x is %d \n", 8 * y / 3 * x);     // 8*3 /3*x = 24/6 will give wrong answer     // 24/3*2     // 8*2     // 16     return 0; }

Arithmetic instructions

 # include<stdio.h> # include <math.h> int main(){     int a = 4;     int b = 8;     printf("The value of a + b is: %d\n", a + b);     printf("The value of a - b is: %d\n", a - b);     printf("The value of a * b is: %d\n", a * b);     printf("The value of a / b is: %d\n", a / b);     int z;     z = b * a; // legal     //b * a = z; // Illegal     printf("The value of z is: %d\n", z);     printf("5 when divided by 2 leaves a remainder of %d\n", 5%2);     printf("-5 when divided by 2 leaves a remainder of %d\n", -5%2);     printf("5 when divided by -2 leaves a remainder of %d\n", 5%-2);     // No operator is assumed to be present     //printf("The value of 4 * 5 is %d\n", (4)(5)); --> Will not return 20/ Wrong!!     printf("The value of 4 * 5 is %d\n", (4)*(5));     // There is no operator to perform ex...

Maths quiz game.c

 #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<time.h> #include<graphics.h> void gameover(){   system("cls");    int gdriver = DETECT,gmode;    initgraph(&gdriver,&gmode,"c:\\TC\\BGI");   setbkcolor(BLACK);   setcolor(GREEN);   settextstyle(1,HORIZ_DIR,10);   outtextxy(100,100,"game");   outtextxy(200,200,"over");   getch();    closegraph(); } void introduction() {   system("cls");    int gdriver = DETECT,gmode;    initgraph(&gdriver,&gmode,"c:\\TC\\BGI");   setbkcolor(GREEN);   setcolor(YELLOW);   settextstyle(1,HORIZ_DIR,7);   outtextxy(100,100,"programmed by ");   outtextxy(200,200," sunil");   getch();    closegraph(); } void start(){   int gdriver= DETECT,gmode;   initgraph(&gdriver,&gmode,"c:\\TC\\BGI");   setbkcolor(BLUE);   setcolor(YELLOW);  // int x...

Snake water gun game

  #include<stdio.h> #include<conio.h> #include<time.h> int results(char you ,char computer){ if (you == computer){ return 0; } if (you == 'w'&&computer =='s'){ return -1; } else if (you == 's'&&computer== 'w'){ return 1; } else if (you == 'w'&&computer== 'g'){ return 1; } else if (you == 'g'&&computer =='w'){ return -1; } else if (you == 's'&&computer =='g'){ return -1; } else if (you == 'g'&&computer =='s'){ return 1; } return 2; } int main(){ char you ,computer ; int result,number; srand(time(0)); number = rand()%100; //system ("cls"); clrscr(); if (number<33){ computer = 'w'; } else if (number>33 && number <66){ computer = 's'; } else{ computer = 'g'; } printf("\nenter 's' for snake 'w' for water and 'g'for gun\n"); scan...

Find simple interest

 #include<stdio.h> int main(){     int principal=100, rate=4, years=1;     int simpleInterest = (principal * rate * years)/100;     printf("The value of simple Interest is %d", simpleInterest);     return 0; }

Celsius to Fahrenheit

 #include<stdio.h> int main(){     float celsius = 37, far;     far = (celsius * 9 / 5) + 32;     printf("The value of this celsius temperature in Fahrenheit is %f", far);     return 0; }

Addition of two numbers

 #include <stdio.h>   int main() {     int a, b;     printf("Enter the value of a\n");     scanf("%d", &a);     printf("Enter the value of b\n");     scanf("%d", &b);     printf("The sum of a and b is %d", a + b);     return 0; }

Keywords example

 #include <stdio.h> /* This is our first c program which is awesome! */ int main() {     int a = 4;     // int b = 8.5; // Not recommended because 8.5 is not an integer     float b = 8.5;     char c = 'u';     int d = 45;     int e = 45 + 4;     printf("The value of a is %d \n", a);     printf("The value of b is %f \n", b);     printf("The value of c is %c \n", c);     printf("Sum of a and d is %d \n", a - d);     printf("Sum of a and d is %d \n", e);     return 0; } // Try it Yourself --> Create a program to add two numbers in C

Area of circle

 #include <stdio.h> int main() {     int radius = 3;     float pi = 3.14;     printf("The area of this circle is %f\n", pi * radius * radius);     int height = 3;     printf("Volume of this cylinder is %f\n", pi * radius * radius * height);     return 0; }

Area of rectangle

#include<stdio.h> int main(){     int length, breadth;     printf("What is the length of the rectangle\n");     scanf("%d", &length);     printf("What is the breadth of the rectangle\n");     scanf("%d", &breadth);     printf("The area of your rectangle is %d", length*breadth);     return 0; }

compare date,year, month, hour, minute, second

  #include<stdio.h> #include<stdlib.h> #include<string.h> #include<conio.h> typedef struct date { int year; int mounth; int date; int hours; int min; int sec; }d; int comp(d c,d c2){ if(c.year<c2.year){ return -1; } if(c.year>c2.year){ return 1; } if(c.mounth<c2.mounth){ return -1; } if(c.mounth>c2.mounth){ return 1; } if(c.date<c2.date){ return -1; } if(c.date>c2.date){ return 1; } if(c.hours<c2.hours){ return -1; } if(c.hours>c2.hours){ return 1; } if(c.min<c2.min){ return -1; } if(c.min>c2.min){ return 1; } if(c.sec<c2.sec){ return -1; } if(c.sec>c2.sec){ return 1; } return 0; } int main(){ d c={20,7,21,6,25,11}; d c2= {2,7,21,6,25,11}; int a = comp(c,c2); system ("cls"); printf("%d\n",a); getch(); return 0; }

bar(); and circle(); function example

  #include<graphics.h> #include<conio.h> int main (){ int gdriver= DETECT ,gmode; initgraph(&gdriver,&gmode,"C:\\TC\\BGI"); bar (100,100,200,400); circle(400,250,50); getch(); return 0; }

Snake game.c

  #include<STDLIB.H> #include<stdio.h> #include<conio.h> #include<time.h> int height =23 ,width =78,x,y,fruitx,fruity,score,gameend,flag; int tailx[100],taily[100]; int piece=0; //x = height and y= width void makelogic(){ int i ; int prevx,prevy,prev2x,prev2y; prevx=tailx[0]; prevy=taily[0]; tailx[0]=x; taily[0]=y; for(i =1;i <=piece;i++){ prev2x=tailx[i]; prev2y=taily[i]; tailx[i]=prevx; taily[i]=prevy; prevx=prev2x; prevy=prev2y; } switch(flag){ case 1: x--; break; case 2: x++; break; case 3: y--; break; case 4: y++; break; } if(x==0||y==0||x==height||y==width){ gameend = 1; } if(x==fruitx && y == fruity){ label3: fruitx = rand ()%23 ; if (fruitx==0){ goto label3; } label4: fruity = rand ()%78 ; if (fruity==0) goto label4; score++; piece++; } } void input(){ ...

Printing colors

Image
  #include<stdio.h> int main (){     int i,c;     //printf("enter the number of rows you want");    // scanf("%d",&n);         for ( i = 0; i<= 10; i++){        // printf("\033[30;1;5m ");          for (c = 0;c <=(10-i);c++){             printf("\033[32;1;3m");             printf(" ");         }         for (c=0;c<=i ; c++){               printf("\033[41;31;5m ");         }         for(c=0;c<=i-1;c++){             printf(" ");   ...

Using of f putc(); and printing A To Z in file

Image
#include<stdio.h> int main(){   FILE *fb;   char ch;   fb = fopen ("file.txt","wt");   for (ch ='A';ch <='Z';ch++){     putc(ch ,fb);     fprintf(fb,"\n");   }      fclose(fb);   return 0; }

Student details enter program

Image
#include<stdio.h> #include<string.h> struct student {     int age;     char name[50];     int r_no;     }; int main(){     int max ;     int i ,p;     printf("enter the total no .of students  ");     scanf("%d",&max);     printf("\n");     struct student s[max];     for (i =0;i <max ;i++){         printf(" enter the name  of the student ");         scanf("%s",s[i].name);          printf("\n");         printf("enter the student age");         scanf("%d",&s[i].age);          printf("\n");         printf("enter the roll num...

Using of typedef struct

Image
  # include < stdio . h > # include < string . h > typedef struct student {     char   name [ 10 ] ;     int sal ; } emp ; int main ( ) {    emp sl ;         printf ( "enter the employee details" ) ;     printf ( "\n" ) ;     printf ( "enter the employee name" ) ;     scanf ( "%[^\n]s" , sl . name ) ;     printf ( "\n" ) ;     printf ( "enter the employee salary" ) ;     scanf ( "%d" , & sl . sal ) ;     printf ( "\n" ) ;     printf ( "employee details" ) ;         printf ( "\n" ) ;     printf ( "%s is employee name" , sl . name ) ;     printf ( "\n" ) ;     printf ( "%d is employee salry " , sl . sal ) ;             }

Using of string with sample example

Image
#include<stdio.h> #include<string.h> int main (){    char str[6] ={'h','e','l','l','o','\0'};     printf("%s",str);         //using strlen     // strlen = string length     printf("\nlength of string is %lu", strlen(str));     //using strcpy     /*strcpy (s1 ,s2)= copy secound string in frist string*/    char str2[6];     strcpy(str2,str);     printf("\n string1 copied in  str2 = %s",str2);         /* using of strncpy     strncpy(s1,s2,length of string to be copied) = copies characters of a string s2 to          another string s1 up to  a speciafied length n */     char str3 [ 10 ] ;     strncpy ( str3 , str , 4 ) ;     printf ( "\n after strncpy str3 is %s " , str...

Reverse the order of number and print

Image
#include<stdio.h> int main(){     int arr[10] ={1,2,3,4,5,6,7,8,9,10};     int front,rear,temp;     front = 0;     rear =9;     while(front<rear){        temp = arr [ front ] ;         arr [ front ] = arr [ rear ] ;         arr [ rear ] = temp ;         front ++ ;         rear -- ;     }     int i ;     for ( i = 0 ; i < 10 ; i ++ ) {         printf ( "\n%d" , arr [ i ] ) ;             }     return 0 ; }