Posts

Showing posts from April, 2022

store a table in dynamic array and then change the size of array dynamically to store more numbers

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

using realloc function in c to dynamically relocate the memory location

  #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 element %d:\n" , i + 1 );         scanf( "%d" , &ptr[i]);     }     //free(ptr);     for ( int i = 0 ; i < 6 ; i++)     {         printf( "The value of element %d is:%d\n" , i + 1 , ptr[i]);     }     ptr=realloc(ptr, 10 * sizeof ( int ));         for ( int i = 0 ; i < 10 ; i++)     {         printf( "Enter the value of element %d:\n" , i + 1 );         scanf( "%d" , &ptr[i]);     }     //free(ptr);     for ( int i = 0 ; i < 10 ; i++)     {         pri...

free dynamic memory allocation

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

dynamic memory using malloc

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

allocating dynamic memory using calloc

  #include <stdio.h> #include <stdlib.h> int main(){ //printf("The size of int in your computer is %d\n",sizeof(int)); // printf("The size of float in your computer is %d\n",sizeof(float)); // printf("The size of char in your computer is %d\n",sizeof(char)); int * ptr; int n; printf( "Enter the size of array:\n" ); scanf( "%d" ,&n); ptr=( int *) calloc(n, sizeof ( int )); for ( int i= 0 ;i<n;i++) {     printf( "Enter the value of element %d:\n" ,i+ 1 );     scanf( "%d" ,&ptr[i]); } for ( int i= 0 ;i<n;i++) {     printf( "The value of element %d is:%d\n" ,i+ 1 ,ptr[i]);     }   return 0 ; }

Entering values in an array using malloc function

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

snake game Version-2 (using random number)

  #include <stdio.h> #include <stdlib.h> #include <time.h> char comp_choice() { int i = 1 , num, gnum;     srand(time( 0 ));     num = rand() % 100 + 1 ;     if (num < 33 )     {         return 'w' ;     }     else if (num< 66 )     {         return 'g' ;     }     else if (num< 100 )     {         return 's' ;     }     } int snakegame( char you, char comp) {     if (comp == you)     {         return 0 ;     }     if (comp == 'w' && you == 's' )     {         return 1 ;     }     else if (comp == 's' && you == 'w' )     {         return - 1 ;     }     if (comp == 'w' && yo...

Snake, gun, water game in C(Version 1)

  #include <stdio.h> int snakegame( char you, char comp) {         if (comp == you)     {         return 0 ;     }     if (comp == 'w' && you == 's' )     {         return 1 ;     }     else if (comp == 's' && you == 'w' )     {         return - 1 ;     }     if (comp == 'w' && you == 'g' )     {         return - 1 ;     }     else if (comp == 'g' && you == 'w' )     {         return 1 ;     }     if (comp == 'g' && you == 's' )     {         return - 1 ;     }     else if (comp == 's' && you == 'g' )     {         return 1 ;     } } int main() { ...

updating number in a file through C program

  #include <stdio.h> int main(){ int num; FILE * ptr; ptr=fopen( "updatefile.txt" , "r" ); fscanf(ptr, "%d" ,&num); printf( "The value of num is %d" ,num); fclose(ptr); ptr=fopen( "updatefile.txt" , "w" ); fprintf(ptr, "%d" , 2 *num);   return 0 ; }

taking input from user and writing in a file in C

  #include <stdio.h> int main(){ FILE *ptr; float sal1,sal2; char name1[ 30 ],name2[ 30 ]; ptr=fopen( "employee.txt" , "w" ); printf( "Enter name of employe 1:\n" ); scanf( "%s" ,name1); printf( "Enter the salary of employee 1:\n" ); scanf( "%f" ,&sal1); printf( "Enter name of employee 2:\n" ); scanf( "%s" ,name2); printf( "Enter the salary of employee 2:\n" ); scanf( "%f" ,&sal2); printf( "%f %f %s %s" ,sal1,sal2,name1,name2); fprintf(ptr, "%s,%f\n" ,name1,sal1); fprintf(ptr, "%s,%f\n" ,name2,sal2); fclose(ptr);   return 0 ; }

reading a character from one file writing it twice in another file

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

writing a table in a file through c program

  #include <stdio.h> void table(FILE *ptr, int a) { for ( int i= 1 ;i<= 10 ;i++) { fprintf(ptr, "%d X %d = %d\n" ,a,i,(a*i)); } } int main(){ FILE * ptr; int a= 8 ; ptr=fopen( "tables.txt" , "w" ); fprintf(ptr, "The table of %d:\n" ,a); table(ptr,a); fclose(ptr);   return 0 ; }

reading 3 integers from a file in C

  #include <stdio.h> int main(){ FILE * ptr; ptr=fopen( "integer_file.txt" , "r" ); int num1,num2,num3; fscanf(ptr, "%d %d %d" ,&num1,&num2,&num3); printf( "The 3 integers are %d %d %d\n" ,num1,num2,num3); fclose(ptr);   return 0 ; }

reading whole file using file functions in C

writing in a text file using c program

  #include <stdio.h> int main(){ FILE * fptr; char c; fptr=fopen( "fputcsample.txt" , "w" ); fputc( 'A' ,fptr); fputc( 'N' ,fptr); fputc( 'C' ,fptr); fputc( 'H' ,fptr); fputc( 'A' ,fptr); fputc( 'L' ,fptr); fclose(fptr);   return 0 ;  

reading a character from file using fgetc

  #include <stdio.h> int main(){ FILE * fptr; char c; fptr=fopen( "fgetcsample.txt" , "r" ); printf( "%c\n" ,fgetc(fptr)); printf( "%c\n" ,fgetc(fptr)); printf( "%c\n" ,fgetc(fptr)); printf( "%c\n" ,fgetc(fptr));   return 0 ;   }

writing in a file in C

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

opening and reading a file in c

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

comparing time using typedef,structure and functions

   #include <stdio.h> typedef struct time{ int hour; int minute; int second; }timestruct; void display(timestruct t1,timestruct t2) {     printf( "First time is:%d:%d:%d\n" ,t1.hour,t1.minute,t1.second);     printf( "Second time is:%d:%d:%d\n" ,t2.hour,t2.minute,t2.second); } int timecomp(timestruct time1, timestruct time2) {     if (time1.hour>time2.hour)     {         return 1 ;     }     if (time1.hour<time2.hour)     {         return - 1 ;     }     if (time1.minute>time2.minute)     {         return 1 ;     }     if (time1.minute<time2.minute)     {         return - 1 ;     }     if (time1.second>time2.second)     {         return 1 ;     }     if (time1.second...

comparing timestamp using typedef,structure and fuctions

  #include <stdio.h> typedef struct timestamp{ int day; int month; int year; int hour; int minute; int second; }tsstruct; void display(tsstruct d1,tsstruct d2) {     printf( "First timestamp is:%d-%d-%d %d:%d:%d\n" ,d1.day,d1.month,d1.year,d1.hour,d1.minute,d1.second);     printf( "Second timestamp is:%d-%d-%d %d:%d:%d\n" ,d2.day,d2.month,d2.year,d2.hour,d2.minute,d2.second); } int timestampcomp(tsstruct ts1, tsstruct ts2) {         if (ts1.year>ts2.year)     {         return 1 ;     }     if (ts1.year<ts2.year)     {         return - 1 ;     }         if (ts1.month>ts2.month)       {         return 1 ;     }     if (ts1.month<ts2.month)     {         return - 1 ;     }     if (ts1.day>ts2.day)...

comparing 2 dates using structures

#include <stdio.h> typedef struct date{ int day; int month; int year; }datestruct; void display(datestruct d1,datestruct d2) {     printf( "First date is:%d/%d/%d\n" ,d1.day,d1.month,d1.year);     printf( "Second date is:%d/%d/%d\n" ,d2.day,d2.month,d2.year); } int datecomp(datestruct date1, datestruct date2) {     if (date1.year>date2.year)     {         return 1 ;     }     if (date1.year<date2.year)     {         return - 1 ;     }     if (date1.month>date2.month)     {         return 1 ;     }     if (date1.month<date2.month)     {         return - 1 ;     }     if (date1.day>date2.day)     {         return 1 ;     }     if (date1.day<date2.day)     {   ...

creating a bank account structure program

  #include <stdio.h> typedef struct bankaccount { int acnum; char name[ 10 ]; char add[ 30 ]; }bank; void display(bank a) {     printf( "The account number is: %d\n" ,a.acnum);     printf( "The name of the account is: %s\n" ,a.name);     printf( "The address is: %s\n" ,a.add);     } int main(){ bank a[ 3 ]; for ( int i= 0 ;i< 3 ;i++) { printf( "Enter the details for account %d\n" ,i+ 1 ); printf( "Enter the account number:\n " ); scanf( "%d" ,&a[i].acnum); printf( "Enter the account name: " ); scanf( "%s" ,a[i].name); printf( "Enter the account Address:\n " ); scanf( "%s" ,a[i].add); } for ( int i= 0 ;i< 3 ;i++) {  display(a[i]); }   return 0 ; }

using typedef and structures creating a complex number program

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

writing a sum function for finding out the sum of vectors

  #include <stdio.h> typedef struct vector {     int x;     int y; }vec; vec sumvector(vec v1,vec v2) {       vec result;   result.x=v1.x+v2.x; result.y=v1.y+v2.y; return result; } int main(){ vec v1,v2; v1.x= 4 ; v1.y= 5 ; printf( "x dim is %d and y dim is %d\n" ,v1.x,v1.y); v2.x= 6 ; v2.y= 5 ; printf( "x dim is %d and y dim is %d\n" ,v2.x,v2.y); vec result; result=sumvector(v1,v2); printf( "The result is %d and %d:" ,result.x,result.y);   return 0 ; }

using typedef and function in structures

#include <stdio.h> #include <string.h> typedef struct employee { int code; float salary; char name[ 10 ]; }emp; void show(emp emp1) { printf( "Code of employee1 is:%d\n" ,emp1.code); printf( "Salary of employee1 is:%0.2f\n" ,emp1.salary); printf( "Name of employee1 is:%s" ,emp1.name); } int main(){ emp e1; emp *ptr; ptr=&e1; ptr->code= 101 ; ptr->salary= 134.34 ; strcpy(ptr->name, "Anchal" ); show(e1);   return 0 ; }

pointer to structure and using structure in a function call

  #include <stdio.h> #include <string.h> struct employee {     int code;     float salary;     char name[ 10 ]; }; void show( struct employee emp) { printf( "Code of employee1 is:%d\n" ,emp.code); printf( "Salary of employee1 is:%0.2f\n" ,emp.salary); printf( "Name of employee1 is:%s" ,emp.name); } int main(){ struct employee e1; struct employee *ptr; ptr=&e1; //(*ptr).code=101;//can also be written as ptr ->code= 101 ; (*ptr).salary= 234.56 ; strcpy(ptr->name, "Anchal" ); show(e1); return ( 0 ); }

Array of structures

  #include <stdio.h> #include <string.h> struct employee {     int code;     float salary;     char name[ 10 ]; }; int main(){ struct employee facebook[ 20 ]; facebook[ 0 ].code= 100 ; facebook[ 0 ].salary= 234.56 ; strcpy (facebook[ 0 ].name, "Anchal" ); facebook[ 1 ].code= 100 ; facebook[ 1 ].salary= 234.56 ; strcpy (facebook[ 1 ].name, "Anchal" ); facebook[ 2 ].code= 100 ; facebook[ 2 ].salary= 234.56 ; strcpy (facebook[ 2 ].name, "Anchal" ); facebook[ 3 ].code= 100 ; facebook[ 3 ].salary= 234.56 ; strcpy (facebook[ 3 ].name, "Anchal" ); printf( "Details for employee 1 are: %d,%f,%s" ,facebook[ 0 ].code,facebook[ 0 ].salary,facebook[ 0 ].name);   return 0 ;

enter data for 3 employees using structures

  #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 Code of employee 1:" );     scanf( "%d" ,&e1.code);     printf( "Enter the Salary of employee 1:" );     scanf( "%f" ,&e1.salary);     printf( "Enter the Name of employee 1:" );     scanf( "%s" ,e1.name);         printf( "Enter the Code of employee 1:" );     scanf( "%d" ,&e2.code);     printf( "Enter the Salary of employee 1:" );     scanf( "%f" ,&e2.salary);     printf( "Enter the Name of employee 1:" );     scanf( "%s" ,e2.name);         printf( "Enter the Code of employee 1:" );     scanf( "%d" ,&e3.code);     printf( "Enter the Salary of employee 1:" ); ...

Basic structure program

#include <stdio.h> #include <string.h> struct employee {     int code;     float salary;     char name[ 10 ]; }; int main(){ struct employee e1; e1.code= 100 ; e1.salary= 234.56 ; strcpy(e1.name, "Anchal" ); printf( "The employee details are as follows:\nCode:%d\nSalary:%.2f\nName:%s" ,e1.code,e1.salary,e1.name);   return 0 ; }

Check if a character is present in the string or not

  #include <stdio.h> void present( char *c, char s) { char * ptr=c; int count= 0 ; while (*ptr!= '\0' ) {     if (*ptr==s)     {         printf( "The character %c is present in the string" ,s);         break ;     }     else     {         printf( "The character %c is not present in the string" ,s);         break ;     }         ptr++; } } int main(){     char s= 'w' ; char str [] = "anchalaggarwal" ; present(str,s);   return 0 ; }

counting the occurrence of a character in a string

  #include <stdio.h> int occurence( char *c, char s) { char * ptr=c; int count= 0 ; while (*ptr!= '\0' ) {     if (*ptr==s)     {         count++;     }     ptr++; } return count; } int main(){     int number; char s= 'a' ; char str [] = "anchalaggarwal" ; number=occurence(str,s); printf( "The occurence of character %c in the string is:%d" ,s,number);   return 0 ; }

Decrypt a string

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