Posts

Showing posts from May, 2021

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