Posts

write a programme to dynamically create an arry to capable of storing integers in c

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

creating snake , water , gun game in c

 #include<stdio.h> #include<stdlib.h> #include<time.h> int SnakeWaterGun(char  you , char  comp){ if(you == comp){ return 0; } if(you=='s' && comp=='g'){ return -1; } else if(you=='g' && comp=='s'){ return  1; } if(you=='s' && comp=='w'){ return  1; } else if(you=='w' && comp=='s'){ return  -1; } if(you=='g' && comp=='w'){ return -1; } else if(you=='w' && comp=='g'){ return  1; }   }   int main() {   char you,comp;   srand(time(0));   int number = rand()%100+1;      if(number<33){   comp = 's';   }    else if(number>33 && number<66){   comp = 'w';   }    else if(number<66){   comp = 'g';   }         printf("Enter 's' for snake , 'g' for gun , 'w' for water\n");   scanf("%c",&you);     int result =  SnakeWaterG...

Create a File & write Data in it in c

 #include<stdio.h> int main() {   FILE *ptr;      char  name[20];   float  salary;      ptr = fopen("Emplyee.txt","w");       if (ptr == NULL)     {         printf("File does not exist.\n");         return 0;     }      printf("Enter the name of the emplyee 1: \n");   scanf("%s", name);   fprintf(ptr, "Name  = %s\n", name);      printf("Emplyee1 salary\n");   scanf("%f", &salary);   fprintf(ptr, "Salary  = %.2f\n", salary);   printf("Enter the name of the emplyee 2: \n");   scanf("%s", name);   fprintf(ptr, "Name  = %s\n", name);      printf("Emplyee2 salary\n");   scanf("%f", &salary);   fprintf(ptr, "Salary  = %.2f\n", salary);     fclose(ptr);      return 0; }

create a num table in a new file in c

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

how to read and write a file in c

 #include<stdio.h> int main() { FILE *fptr; int num = 45; fptr = fopen("generated.txt", "w"); fprintf(fptr,"The number is %d\n",num); fprintf(fptr , "thank you baby\n"); fclose(fptr);   return 0; } #include<stdio.h>   int main() {   FILE *ptr;     int num;   ptr = fopen("nilabhrudas.txt","r");   fscanf(ptr,"%d\n",&num);   fprintf("The value of the num is %d\n", num);   return 0; } #include<stdio.h> int main() {   FILE *ptr;   char c;   ptr = fopen("nayan.txt","r");   c = fgetc(ptr);   while(c!=EOF){   printf("%c",c);   c = fgetc(ptr);   }   return 0; }

write a programme to compare time stamp in c

 #include<stdio.h> #include<string.h> typedef struct time_stamp{ int year; int month; int date; int hour; int minute; int seacond; }time_stamp; void display(time_stamp dt1,time_stamp dt2){ printf("the time stamp is: %d/%d/%d-%d:%d:%d\n",dt1.year,dt1.month,dt1.date,dt1.hour,dt1.minute,dt1.seacond); //} //void display(time_stamp dt2){ printf("the time stamp is: %d/%d/%d-%d:%d:%d\n",dt2.year,dt2.month,dt2.date,dt2.hour,dt2.minute,dt2.seacond); } int time_stampcmp(time_stamp dt1,time_stamp dt2){ if(dt1.hour>dt2.hour){ return 1; } if(dt1.hour<dt2.hour){ return -1; } if(dt1.minute>dt2.minute){ return 1; } if(dt1.minute<dt2.minute){ return -1; } if(dt1.seacond>dt2.seacond){ return 1; } if(dt1.seacond<dt2.seacond){ return -1; }     if(dt1.year>dt2.year){ return 1; } if(dt1.year<dt2.year){ return -1; } if(dt1.month>dt2.month){ return 1; } if(d...

write a struct programme to keep bank details in c

#include<stdio.h> #include<string.h> struct customer{ int account_num; int IFSE_code; float Balance; char name[10]; }; int main() {    struct customer n1 = {6066,58741,9854123,"nilabhru"};    printf("%d\n",n1.account_num);  printf("%d\n",n1.IFSE_code);  printf("%0.3f\n",n1.Balance);  printf("%s\n",n1.name);   return 0; } 

write down a display programme for complex numbers in c

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

Measuring string lenth in c

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

how to encrypt and decrypt a data in c

 #include<stdio.h> void encrypt(char *c){    char *ptr = c;    while(*ptr!= '\0'){     *ptr = *ptr + 2;     ptr++;    } } int main() { char c[] = "nilabhru come on amd fuck me"; encrypt(c); printf("Encrypted string is: %s",c); return 0; } #include<stdio.h> void decrypt(char *c){    char *ptr = c;    while(*ptr!= '\0'){     *ptr = *ptr - 2;     ptr++;    } } int main() { char c[] = "pkncdjtw eqog qp cof hwem og"; decrypt(c); printf("Decrypted string is: %s",c); return 0; }

structure formate in c

 #include<stdio.h> #include<string.h> struct employee{ int code; float salary; char name[10]; }; int main() {    struct employee e1 = {100,3645285,"nilabhru"};  /*struct employee e1;  e1.code = 100;  e1.salary = 34.542;*/   // strcpy(e1.name , "Nilabhru");    printf("%d\n",e1.code);  printf("%0.3f\n",e1.salary);  printf("%s\n",e1.name);   return 0; }

Usage of strcpy, strcat, strcmp (strings) in c

 #include<stdio.h> #include<string.h> int main() { char *st = "Nilabhru"; char st2[45]; strcpy(st2,st); printf("now the st2 is %s",st2); return 0; } #include<stdio.h> #include<string.h> int main() { char st1[45] = "Hello"; char *st2 = "Nilabhru"; strcat(st1,st2); printf("now the st2 is %s",st1); return 0; } #include<stdio.h> #include<string.h> int main() { char st1[45] = "Hello"; char *st2 = "Nilabhru"; int val = strcmp(st1,st2); printf("now the st2 is %d",val); return 0; }

Reverse printing in c

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

Making tabels with arry in c

 #include<stdio.h> int main() {    int mul[10],i,n;        printf("Enter the number which table you want\n");    scanf("%d", &n);        for( i=0; i<10; i++){     mul[i] = n*(i+1);    }    for( i=0; i<10; i++){     printf("%d*%d=%d\n",n,i+1,mul[i]);    } return 0; }

Making 2 dimentional arry in c

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

printing value for arry in c

#include<stdio.h> int printArry(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]= 555;//This value will be change in arry of main as well } int main() {     int arr[] = {45,445,4546,44,465};         printArry(arr,5) ;     printf("%d",arr[2]);     return 0; } 

print markes using Arry in c

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

swaping value by using pointers in c

 #include<stdio.h> int 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);    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; } int swap ( int *a, int *b){ int temp; temp = *a; *a = *b; *b = temp; }

calculate sum and average with pointers in c

 #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; printf("Enter the value of i %d"); scanf("%d", &i);     printf("Enter the value of j %d"); scanf("%d", &j);     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; }

draw a star pattern in c

 #include<stdio.h> int main() {   int i,j,r,k=0;      printf("Enter number of rows:");   scanf("%d",&r);      for(i=1;i<=r;++i,k=0)   {   for(j=1;j<=r-i;++j)     {   printf(" ");     }   while(k != 2*i-1)   {   printf("*");   ++k;   }   printf("\n");    }  return 0; }