0% found this document useful (0 votes)
137 views

C Programes

The document contains 26 C programming questions and their solutions ranging from basic programs like printing name and address to more complex programs involving calculations, conditional statements, loops etc. The programs cover topics like data input/output, arithmetic operations, conditional logic, loops, functions, data types etc. Each question has the complete C code to solve the given programming problem.

Uploaded by

Modi Mitulkumar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
137 views

C Programes

The document contains 26 C programming questions and their solutions ranging from basic programs like printing name and address to more complex programs involving calculations, conditional statements, loops etc. The programs cover topics like data input/output, arithmetic operations, conditional logic, loops, functions, data types etc. Each question has the complete C code to solve the given programming problem.

Uploaded by

Modi Mitulkumar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 49

C Programmes for practical

1. Program to print your name. (Without reading from keyboard) 2. Write a program to print your Name, Address, and Phone no. as under: Name : Address : Phone No. : 3. Write a program to draw following rectangle using simple printf statement. --------------------------------------------------| | | | | | --------------------------------------------------4. Program to print your name and address in box. #include<stdio.h> #include<conio.h> void main() { clrscr(); printf(" --------------------"); printf("\n| Name: keyur |"); printf("\n______________"); getch();

} 5. Program to read your age from keyboard and print it. #include<stdio.h> #include<conio.h> void main() { int a; clrscr();

printf("enter your age"); scanf("%d",&a); printf("your age is %d",a); getch(); } 6. Program to determine the average of three numbers #include<stdio.h> #include<conio.h> void main() { int a,b,c,sum,avg; clrscr(); printf("a="); scanf("%d",&a); printf("b="); scanf("%d",&b); printf("c="); scanf("%d",&c); sum=a+b+c; avg=sum/3; printf("average is %d",avg); getch(); }
7.

Program to swap a value of two variable without using third variable. #include<stdio.h> #include<conio.h> void main() { int a,b;

clrscr(); printf("a="); scanf("%d",&a); printf("b="); scanf("%d",&b); printf("before swap value of a= %d",a); printf("\nbefore swap value of b= %d",b); a=a-b; b=a+b; a=b-a; printf("\n\nafter swap a=%d",a); printf("\nafter swap b=%d",b); getch(); } 8. Write a prog. To find addition,subtraction,muliplicaiton,division and modular division of two numbers #include<stdio.h> #include<conio.h> void main() { int a,b,sum,mul,div,sub; clrscr(); printf("a="); scanf("%d",&a); printf("b="); scanf("%d",&b); sum=a+b; mul=a*b; div=a/b; sub=a-b; printf("sum is=%d",sum); printf("\nmul is=%d",mul); printf("\ndiv is=%d",div);

printf("\nsub is=%d",sub); getch();

} 9. Write a prog. To find Celsius from Farenhit #include<stdio.h> #include<conio.h> void main() { float f,a,b,c; clrscr(); printf("faranhit="); scanf("%f",&f); a=f-32; c=a*0.56; printf("celsius=%f",c); getch(); } 10. Write a prog. To find roots of a quadratic equation #include<stdio.h> #include<math.h> void main() { int A,B,C; float disc,deno,x1,x2; clrscr(); printf("\n-------------------------------------------------------"); printf("\n\n PROGRAM TO FIND THE ROOTS OF A QUADRATIC EQUATION ");

printf("\n\n-------------------------------------------------------"); printf("\n\n\t ENTER THE VALUES OF A,B,C..."); scanf("%d,%d,%d",&A,&B,&C); disc = (B*B)-(4*A*C); deno = 2*A; if(disc > 0) { printf("\n\t THE ROOTS ARE REAL ROOTS"); x1 = (-B/deno)+(sqrt(disc)/deno); x2 = (-B/deno)-(sqrt(disc)/deno); printf("\n\n\t THE ROOTS ARE...: %f and %f\n",x1,x2); } else if(disc == 0) { printf("\n\t THE ROOTS ARE REPEATED ROOTS"); x1 = -B/deno; printf("\n\n\t THE ROOT IS...: %f\n",x1); } else printf("\n\t THE ROOTS ARE IMAGINARY ROOTS\n"); printf("\n-------------------------------------------------------"); getch(); }

11.

Write a prog. To convert days into year, month and remaining days #include<stdio.h> #include<conio.h> void main()

{ int m=0,y=0,d; clrscr(); printf("enter days="); scanf("%d",&d); m=d/30; y=m/12; d=d%365;

printf("Month=%d",m); printf("\nYear=%d",y); printf("\nDays=%d",d); getch(); } 12. Program to determine the simple intrest and compound intrest. Also determine the differnce between them. [Eq. of simple intrest = p * r * n /100 & compount interest=(p * (1 + r/100)n)-p] #include<stdio.h> #include<conio.h> void main() { int a,r,y,I,C; clrscr(); printf("Enter amount="); scanf("%d",&a); printf("Enter rate="); scanf("%d",&r); printf("Enter year="); scanf("%d",&y);

I=(a*r*y)/100; printf("Interest is=%d",I); C=(a*(pow(1+r/100,y)))-a; printf("Compound Interest is=%d",C); getch(); } 13. Program to determine the area of Cylinder, Sphere, Circle, Ball, Triangle, Rectangle. (Here you must have to use power function) [Eq. of area of Cylinder=2 * pi * r * h area of Sphere=(4/3) * pi * r3 area of Circle=pi * r2 area of Ball=4 * pi * r2 area of Triangle=(1/2) * b * h area of Rectangle=l * b ] #include<stdio.h> #include<conio.h> void main() { int r,h,b,l,C,S,s,c,B,T,t,R,pi=3.14; clrscr(); printf("Enter radius="); scanf("%d",&r); printf("\nEnter height="); scanf("%d",&h); printf("\nEnter b="); scanf("%d",&b); printf("\nEnter length="); scanf("%d",&l); C=2*pi*r*h; s=4/3; S=s*pi*r*r*r; c=pi*r*r;

B=4*pi*r*r; t=1/2; T=t*b*h; R=l*b; printf("\n\nArea of Cylinder=%d",C); printf("\nArea of Sphere=%d",S); printf("\nArea of Circle=%d",c); printf("\nArea of Ball=%d",B); printf("\nArea of Triangle=%d",T); printf("\nArea of Rectangle=%d",R); getch(); }
14.

Write a program to determine the salary of employee from the basic salary on the base of following allowance and deduction. Allowance :D.A. 25 % H.R.A. Medical 10 % 05 %

Travelling 03 % Deduction :P.F. P.T. 10 % 02 % 10 %

Income tax

Determine the Gross salary and Net salary of employee. #include<stdio.h> #include<conio.h> void main() { float b,DA,HRA,M,T,PF,PT,IT,TS,All,Ded,da; clrscr(); printf("Enter Basic Salary=");

scanf("%f",&b);

DA=b*0.25; HRA=b*0.10; M=b*0.05; T=b*0.03; All=DA+HRA+M+T;

printf("\n\nYour allowance=%f",All);

PF=b*0.10; PT=b*0.02; IT=b*0.10; Ded=PF-PT-IT;

printf("\nYour Deduction =%f",Ded);

TS=b+All+Ded; printf("\n\nYour Total Salary=%f",TS); getch(); }


15.

WAP to check whether the no is even or odd? [e.g. 2,4,6,8 are even nos] #include<stdio.h> #include<conio.h> void main() { int n; clrscr();

printf("Enter Number="); scanf("%d",&n); if(n%2==0) { printf("Number is Even"); } else { printf("Number is Odd"); } getch(); } 16. Write a prog. To find maximum no. from any three nos. #include<stdio.h> #include<conio.h> void main() { float a,b,c; clrscr(); printf("a="); scanf("%f",&a);

printf("b="); scanf("%f",&b);

printf("c="); scanf("%f",&c);

if(a>b && a>c) { printf("Maximum No=%f",a); } else if(b>a && b>c) { printf("Maximum NO=%f",b); } else { printf("Maximum No=%f",c); } getch(); }
17.

Write a prog. To find maximum no. from given three nos. using conditional operator #include<stdio.h> #include<conio.h> void main() { int d,max,a,b,c; clrscr(); printf("a="); scanf("%d",&a);

printf("b="); scanf("%d",&b);

printf("c=");

scanf("%d",&c); d=a>b? a:b; max=d>c? d:c; printf("Maximum No=%d",max); getch(); } 18. Write A Programme to find out all the numbers divisible by 5 and 7 between 1 to 100. #include<stdio.h> #include<conio.h> void main() { int n; clrscr(); for(n=1;n<=100;n++) if(n%5==0 || n%7==0) { printf("\nNumbers divisible by 5 and 7 are=%d",n); } getch(); } 19. Write A Programme that reads number from 1 to 7 and accordingly it should display MONDAY to SUNDAY (switchcase). #include<stdio.h> #include<conio.h> void main() { int ch; clrscr();

while(1) { printf("\nenter your choice::"); scanf("%d",&ch); switch(ch) { case 1: printf("Monday\n"); break;

case 2: printf("Tuseday\n"); break;

case 3: printf("Wednesday\n"); break;

case 4: printf("Thursday\n"); break;

case 5: printf("Friday\n"); break;

case 6: printf("Saturday\n"); break;

case 7: printf("Sunday\n"); break;

case 8: exit(0);

break;

default : printf("wrong choice"); break; } } getch(); } 20. Write A Programme to make a simple calculator.(using switchcase).

#include<stdio.h> #include<conio.h> void main() { int a,b,sum,div,mul,sub; int ch; clrscr(); while(1) { printf("1 for Addition\n"); printf("2 for Multiplication\n"); printf("3 for Division\n"); printf("4 for Subtration\n"); printf("5 for Exit\n"); printf("Enter your choice="); scanf("%d",&ch); switch(ch) {

case 1: printf("a="); scanf("%d",&a); printf("b="); scanf("%d",&b); sum=a+b; printf("\nSum=%d\n",sum); break; case 2: printf("a="); scanf("%d",&a); printf("b="); scanf("%d",&b); mul=a*b; printf("\nMul=%d\n",mul); break; case 3: printf("a="); scanf("%d",&a); printf("b="); scanf("%d",&b); div=a/b; printf("\nDiv=%d\n",div); break; case 4: printf("a="); scanf("%d",&a); printf("b="); scanf("%d",&b); sub=a-b; printf("\nSub=%d\n",sub); break;

case 5: exit(0); break; default: printf("wrong choice"); break; } } getch(); } 21. Write a prog. To find the entered no is positive or negative #include<stdio.h> #include<conio.h> #include<math.h> void main() { float n; clrscr(); printf("Enter Number="); scanf("%f",&n); if(n<0) { printf("\n\nNumber is Negetive"); } else if(n>0) { printf("\n\nNumber is Positive"); } else {

printf("\n\nNumber is Zero"); } getch(); } 22. WAP to check whether the no is prime or not? [e.g. are prime] #include<stdio.h> #include<conio.h> main() { clrscr(); int a,c=0,i,n; printf("enter the number to be checked="); scanf("%d",&n); for(i=1;i<=n;i++) { a=n%i; if(a==0) { c=c+1; } } if (c==2) { printf("the given number is prime"); } else { printf("the given number is not prime"); } 5, 11, 13

getch();
23.

}WAP to check whether the no is Armstrong no or not? e.g. 153 is Armstrong As 13 + 53+33=153] #include<stdio.h> #include<conio.h> void main() { int n,sum=0,i,x,a; clrscr(); printf("Enter Number="); scanf("%d",&n); x=n; for(i=1;n>0;i++) { a=n%10; sum=sum+a*a*a; n=n/10; } if(sum==x) { printf("Armstrong"); } else printf("Not Armstrong"); getch(); }

24. Write a prog. To find whether the entered no. is perfect or not. (i.e sum of digits equals to multiplication of digits) #include<stdio.h>

#include<conio.h> void main() { int n,i,sum=0,mul=1,a; clrscr(); printf("Enter No="); scanf("%d",&n); for(i=1;i<n;i++) { a=n%10; sum=sum+a; mul=mul*a; n=n/10; } if(sum==mul) { printf("no is perfect"); } else { printf("no is not perfect"); } getch(); }
25.

WAP to sum upto the given no.

[i.e.

1+2+3++N]

#include<stdio.h> #include<conio.h>

void main() { int i,sum=0,n; clrscr(); printf("Enter Numbers="); scanf("%d",&n); for(i=1;i<=n;i++) { sum=sum+i; } printf("SUM is=%d",sum); getch(); }
26.

WAP to find the factorial of given no. [i.e. n!=n*(n-1)*(n-2) ..*1] #include<stdio.h> #include<conio.h> void main() { int n,a,fact=1; clrscr(); printf("Enter Number="); scanf("%d",&n); for(a=1;a<=n;a++) { fact=fact*a; } printf("Factorial is=%d",fact);

getch(); } 27. Write A Programme to find out the total number of odd digits within the given number and print the sum of all odd digits.

#include<stdio.h> #include<conio.h> void main() { int n,i,sum=0,count=0; clrscr(); printf("Enter number="); scanf("%d",&n); for(i=0;i<=n;i++) { if(i%2==0)

continue; count++; sum=sum+i; } printf("\n total of odd digits=%d",sum); printf("\n total no of odd digits=%d",count); getch(); } 28. Write A Programme to reverse a number. #include<stdio.h> #include<conio.h> void main()

{ int a,i,rev=0,n; clrscr(); printf("Enter No="); scanf("%d",&n); for(i=0;i<=n;i++) { a=n%10; rev=rev*10+a; n=n/10; } printf("Reverse No=%d",rev); getch(); }

29.

Write a Program to find out largest digit in a given integer e.g. 12345, 5 is the greatest. #include<stdio.h> #include<conio.h> void main() { int n,i,a,max=0; clrscr(); printf("Enter No="); scanf("%d",&n); for(i=1;i<=n;i++) { a=n%10; if(a>max)

{ max=a; } n=n/10; } printf("Largest no=%d",max); getch(); } 30. Write a prog. To check whether the given no. is binary or not #include<stdio.h> #include<conio.h> void main() { int n,m,mod,rev=0; clrscr(); printf("enter no="); scanf("%d",&n); //m=n; while(n>0) { mod=n%10; rev=(rev*10)+mod; if(mod>1) { goto x; } n=n/10; }

if(mod>=0) { printf("\n no is binary"); } else { x:printf("\n no is not binary"); } getch(); } 31.Write A Programme to check whether the given number is octal or not. #include<stdio.h> #include<conio.h> void main() { int n,m,mod,rev=0; clrscr(); printf("enter no="); scanf("%d",&n); m=n; while(n>0) { mod=n%10; rev=(rev*10)+mod; n=n/10; } if(mod<=7) {

printf("\n no is octal"); } else { printf("\n no is not octal"); } getch(); } 32.Write A Programme to calculate fibonacii series no 1 1 2 3 5 8 #include<stdio.h> #include<conio.h> void main() { int i,n,n1=0,n2=1,n3; clrscr(); printf("Enter no="); scanf("%d",&n); printf("%d%d",n1,n2); for(i=1;i<=n;i++) { n3=n1+n2; n1=n2; n2=n3; printf("%d",n3); } getch(); }

33.Write A Programme to print first 50 odd numbers. Note that program should display only five numbers per line. #include<stdio.h> #include<conio.h> void main() { int i; clrscr(); for(i=0;i<=100;i++) if(i%2!=0) { printf("%d\t\t",i); } getch(); }
$$$$$

34. Write A Programme for :

$$$$ $$$ $$ $

#include<stdio.h> #include<conio.h> void main() { int i,n,j; clrscr(); for(i=5;i>=1;i--) { for(j=i;j>=1;j--)

{ printf("$"); } printf("\n"); } getch(); } 35. Write A Programme for :


54321 4321 321

#include<stdio.h> #include<conio.h> void main() { int i,j; clrscr(); for(i=5;i>=1;i--) { for(j=i;j>=1;j--) { printf("%d",j); } printf("\n"); } getch(); } 36. Write A Programme for :
5 4 4 3 3 2 1 1 2 2 1 3 2 1

#include<stdio.h> #include<conio.h> #include<math.h> void main() { int a,b,c,d,e; clrscr(); for(a=5;a<6;a++) { printf(" } printf("\n"); %d",a);

for(b=4;b<5;b++) { printf(" } printf("\n"); %d %d",b,b);

for(c=3;c<4;c++) { printf(" %d %d %d",c,c,c); } printf("\n");

for(d=2;d<3;d++)

{ printf(" %d %d %d %d",d,d,d,d); } printf("\n");

for(e=1;e<2;e++) { printf("%d %d %d %d %d",e,e,e,e,e); } getch(); } 37. Write A Programme for :


E DE CDE BCDE ABCDE

#include<stdio.h> #include<conio.h> void main() { int i,j; clrscr(); for(i=69;i>=65;i--) { for(j=65;j<=i;j++) { printf(" ");

} for(j=i;j<=69;j++) printf("%c",j); printf("\n"); } getch(); }

38.Write a prog. For following triangle. 1 01 101 0101 10101

#include<stdio.h> #include<conio.h> void main() { int i,j; clrscr(); for(i=1;i<=5;i++) { for(j=1;j<=i;j++) { if((i+j)%2==0) printf("1"); else

printf("0"); } printf("\n"); } getch(); } 39. Write A Programme to find : x + x^2 + x^3 + x^4 +..+ x^n #include<stdio.h> #include<conio.h> #include<math.h> void main() { int sum=0,i,n,x; clrscr(); printf("x="); scanf("%d",&x); printf("\ni="); scanf("%d",&i); for(n=1;n<=i;n++) { sum=sum+pow(x,n); } printf("%d",sum); getch(); } 40. Write a prog. To find following series. 1 + 22 + 33 +.. #include<stdio.h> #include<conio.h>

#include<math.h> void main() { int i,j,sum=0,n; clrscr(); printf("enter n="); scanf("%d",&n); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { sum=sum+pow(i,j); i++; }

printf("%d",sum); } getch(); } 41. Write a prog. To find following series 1 2 + 3 4 + 5 6 #include<stdio.h> #include<conio.h> #include<math.h> void main() { int sum=0,n,i; clrscr(); printf("\n Enter the no=:"); +

scanf("%d",&n); for(i=1;i<=n;i++) { if(i%2==0) { sum=sum-i; } else { sum=sum+i; } } printf("\n Sum=%d",sum); getch(); } 42. Write a prog. To find following series 1 + 1/2 + 1/3 + 1/4 + .. #include<stdio.h> #include<conio.h> void main() { float i,n; float sum; sum=0;

clrscr(); printf("enter no="); scanf("%f",&n); for(i=1;i<=n;i++) {

sum=sum+(1/i); } printf("%f",sum); getch(); } 43. Write A Programme to add corresponding elements of two 1-D array and store them in third 1-D array #include<stdio.h> #include<conio.h> void main() { int a1[2],a2[2],a3[2]; int i,j,n1,n2; clrscr(); printf("Array 1=\n");

for(i=0;i<2;i++) { scanf("%d",&a1[i]); } printf("Array 2=\n");

for(i=0;i<2;i++) { scanf("%d",&a2[i]); } for(i=0;i<2;i++) { a3[i]=a1[i]+a2[i];

printf("Array 3=%d",a3[i]); } getch(); }

44. Write A Programme to find maximum element from 1-D array. #include<stdio.h> #include<conio.h> void main() { int i,n; int num[10],large,small; clrscr(); printf("Enter a Number=\n"); scanf("%d",&n); for(i=0;i<n;i++) { printf("\n Enter Number for %d element=",i); scanf("%d",&num[i]); large=num[i]; small=num[i]; } for(i=0;i<n;i++) { if(num[i]>large) large=num[i]; else if(num[i]<small) small=num[i];

} printf("\n largest element is %d",large); printf("\n Smallest element is %d",small); getch(); } 45. Write A Programme to find number of odd and even elements from the 1-D array. #include<stdio.h> #include<conio.h> void main() { int i,n; int num[10],even,odd; clrscr(); printf("Enter a Number=\n"); scanf("%d",&n); for(i=0;i<n;i++) { printf("\n Enter Number for %d element=",i); scanf("%d",&num[i]); even=num[i]; odd=num[i]; } for(i=0;i<n;i++) { if(num[i]%2==0) { even=num[i]; }

else odd=num[i]; } printf("\n Even element is %d",even); printf("\n Odd element is %d",odd); getch(); } 46. Write A Programme to find maximum value from given 2-D array. #include<stdio.h> #include<conio.h> void main() { int i,j,n; int num[10][10],large,small; clrscr(); printf("Enter a Number=\n"); scanf("%d",&n); for(i=0;i<n;i++) { for(j=0;j<n;j++) { printf("\n Enter Number for %d%d element=",i,j); scanf("%d",&num[i][j]); large=num[i][j]; small=num[i][j]; } } for(i=0;i<n;i++)

{ for(j=0;j<n;j++) { if(num[i][j]>large) large=num[i][j]; else if(num[i][j]<small) small=num[i][j]; } } printf("\n largest element is %d",large); printf("\n Smallest element is %d",small); getch(); } 47. Write a prog. To add two matrices #include<stdio.h> #include<conio.h> #include<math.h> void main() { int a[3][3],b[3][3],c[3][3],i,j; clrscr(); printf("enter matrix a="); for(i=0;i<=3;i++) { for(j=0;j<=3;j++) scanf("%d",&a[i][j]); } printf("enter matrix b=");

for(i=0;i<=3;i++) { for(j=0;j<=3;j++) scanf("%d",&b[i][j]); } for(i=0;i<=3;i++) { for(j=0;j<=3;j++) c[i][j]=a[i][j]+b[i][j]; } for(i=0;i<=3;i++) { for(j=0;j<=3;j++) printf("%d",c[i][j]); } printf("\n"); getch(); } 48. Write a prog. To read two matrices, A and B and find & print A * B. #include<stdio.h> #include<conio.h> #include<math.h> void main() { int a[3][3],b[3][3],c[3][3],i,j,n,k; clrscr(); printf("enter elements="); scanf("%d",&n);

printf("matrix a::"); for(i=0;i<n;i++) { for(j=0;j<n;j++) scanf("%d",&a[i][j]); } printf("matrix b="); for(i=0;i<n;i++) { for(j=0;j<n;j++) scanf("%d",&b[i][j]); } printf("c=a*b\n"); for(i=0;i<n;i++) { for(j=0;j<n;j++) { c[i][j]=0; for(k=0;k<n;k++) { c[i][j]+=a[i][k]*b[k][j]; } printf("%d",c[i][j]); } printf("\n"); } getch(); } 49. Write A Programme to sort given array in descending order.

#include<stdio.h> #include<conio.h> void main() { int i,j,n,a[20],temp; clrscr(); printf("Howmany numbers you want to enter::"); scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d",&a[i]); } printf("Sorted number in descending order::\n"); for(j=0;j<n-1;j++) { for(i=0;i<n-1-j;i++) { if(a[i]<a[i+1]) { temp=a[i]; a[i]=a[i+1]; a[i+1]=temp; } } } for(i=0;i<n;i++) { printf("%d",a[i]);

} getch(); } 50. Write A Programme using function to find out maximum out of three numbers. #include<stdio.h> #include<conio.h> void main() { int a,b,c,d; void main(int a,int b,int c); clrscr(); printf("A=\n"); scanf("%d",&a); printf("B=\n"); scanf("%d",&b); printf("C=\n"); scanf("%d",&c); max(a,b,c); // printf("Maximum No =%d\n",d); getch(); } int max(int a,int b,int c) { if(a>b && a>c) { printf("Maximum=%d",a); } else if(b>a && b>c)

{ printf("Maximum=%d",b); } else { printf("Maximum=%d",c); } } 51. Write a prog. To find out factorial no. of given no.(Using recursion) #include<stdio.h> #include<conio.h> #include<math.h> void main() { int factorial(int); int n; clrscr(); printf("Enter a number="); scanf("%d",&n); printf("\nFactorial of %d is=%d",n,factorial(n)); getch(); } int factorial(int f) { int fact; if(f==1) return(1); else

fact=f*factorial(f-1); return(fact); }

52. Write a prog. For swapping of two values(Using Function & pointers) #include<stdio.h> #include<conio.h> void main() { int a,b; clrscr(); printf("a="); scanf("%d",&a); printf("b="); scanf("%d",&b); swap(a,b); //printf("%d%d",a,b); getch(); } swap(int *x,int *y) { int t; t=*x; *x=*y; *y=t; printf("b=%d\n",x); printf("a=%d",y); }

53. Write a prog. To display information of 5 students which includes roll_no, name and age using structures. #include<stdio.h> #include<conio.h> struct stud { char name[5]; int rollno; int age; }s[30]; void main() { int i; clrscr(); for(i=0;i<5;i++) scanf("%s%d%d",s[i].name,&s[i].rollno,&s[i].age); for(i=0;i<5;i++) printf("%s\t\n%d\t\n%d\t\n",s[i].name,s[i].rollno,s[i].age); getch(); } 54. Write a prog. To read and print array of n elements using pointers #include<stdio.h> #include<conio.h> void main() {

int a[100],n,i; int *ptr; clrscr(); ptr=a; printf("enter element n="); scanf("%d",&n); printf("enter values::"); for(i=0;i<n;i++) { scanf("%d",ptr); ptr++; } ptr=a; printf("given values are"); for(i=0;i<n;i++) { printf("%d",*ptr); ptr++; } getch(); }

55. Write a prog. that reads student data (roll_no, name and age)from keyboard for 5 students and write them into file. The prog. Then reads the data from the same file and display on screen Writing into file:: #include<stdio.h> #include<conio.h>

#include<stdlib.h> struct student { int rollno; int age; char name[20]; }s[5]; void main() { FILE *p; int i; clrscr(); p=fopen("b.txt","w"); if(p==NULL) { printf("file cant be open"); exit(0); } for(i=0;i<5;i++) { printf("\n entre record of student"); scanf("%d",&s[i].rollno); scanf("%d",&s[i].age); fflush(stdin); gets(s[i].name); fprintf(p,"%d%d%s",s[i].rollno,s[i].age,s[i].name); } fclose(p);

getch(); } Reading from file:: #include<stdio.h> #include<conio.h> #include<stdlib.h> struct student { int rollno; int age; char name[20]; }s[5]; void main() { FILE *p; int i; clrscr(); p=fopen("b.txt","r"); if(p==NULL) { printf("file cant be open"); exit(0); } for(i=0;i<5;i++) { fscanf(p,"%d %d %s",&s[i].rollno,&s[i].age,s[i].name); printf("\n rollno=%d age=%d name=%s",s[i].rollno,s[i].age,s[i].name); }

fclose(p); getch(); }

You might also like