Posts

Showing posts from July, 2021

Union in C

 1. Write a C program to find the size of total memory allocated when using union. #include<stdio.h> union emp {     int id;     char name[10];     char address[15]; }; int main() {     union emp e;     printf("The maximum size of data is %d",sizeof(union emp));     return 0; }

Array of Structure

 1. Write a program to display value of 5 students using array of structure. #include<stdio.h> #include<string.h> struct student {     int id;     char name[20];     char add[20]; }; int main() {     int i;     struct student st[5];     for (i=1;i<=5;i++)     {         printf("Enter the id?");         scanf("%d",&st[i].id);         printf("Enter the name?");         scanf("%s",&st[i].name);         printf("Enter the address?");         scanf("%s",&st[i].add);     }     printf("List of data");     for(i=0;i<=5;i++)     {         printf("Roll no %d is %s whose address is %s \n",st[i].id,st[i].name,st[i].add);     }     return 0; }

Structure example

 1. Write a c program to display name and price of car using structure. #include<stdio.h> int main() {     struct car     {         char name[20];         int price     };     struct car c1={"Lambo",2000};     printf("The name of car is %s \n",c1.name);     printf("The price of car is %d",c1.price);     return 0; } or #include<stdio.h> struct car     {         char name[20];         int price;     }c1; int main() {     strcpy(c1.name,"lambo"); // copying string in char array     c1.price=12345;     printf("The name of car is %s",c1.name);     printf("The price of car is %d",c1.price);     return 0; } structure example with asking input from user #include<stdio.h> int main() {     struct emp     {         char...

Recursion example

  1. C Program to find the factorial of given number. #include<stdio.h> #include<conio.h> int main() {     int i,f=1,n;     printf("Enter the factorial number.");     scanf("%d",&n);     for(i=1;i<=n;i++)     {         f=f*i;     }     printf("The factorial of %d is %d",n,f);     return 0; } 2. C Program to find the factorial using recursion. #include<stdio.h> #include<conio.h> int fact(int n); int main() {     int x,f;     printf("Enter the number whose factorial you want to find?");     scanf("%d",&x);     f=fact(x);     printf("The factorial of %d is %d",x,f);     return 0; } int fact(int n) {     if(n==1)     {         return 1;     }     else if(n==0)     {         return 0;     }  ...
 1. C Program to find the factorial of given number. #include<stdio.h> #include<conio.h> int main() {     int i,f=1,n;     printf("Enter the factorial number.");     scanf("%d",&n);     for(i=1;i<=n;i++)     {         f=f*i;     }     printf("The factorial of %d is %d",n,f);     return 0; }