Sei sulla pagina 1di 69

/*1. Write a C program to print no of bytes of memory occupied by different types of variables using size operator*/ #include<stdio.h> #include<conio.

h> main() { int i; float f; double d; long int l; char ch; unsigned int u; clrscr(); printf("integer type variable occupies %d bytes of memory",sizeof(i)); printf("float type variable occupies %d bytes of memory",sizeof(f)); printf("double type variable occupies %d bytes of memory",sizeof(d)); printf("longint type variable occupies %d bytes of memory",sizeof(l)); printf("unsigned integer type variable occupies %d bytes of memory",sizeof(u)); printf("charecter type variable occupies %d bytes of memory",sizeof(ch)); } OUTPUT: integer type variable occupies 2 bytes of memory float type variable occupies 4 bytes of memory double type variable occupies 8 bytes of memory longint type variable occupies 4 bytes of memory unsignedinteger type variable occupies 2 bytes of memory charecter type variable occupies 1 bytes of memory

2.Write a C program to demonstrate mathematical library functions available in C library*/ #include<stdio.h> #include<conio.h> #include <math.h> main() { int deg,val,res; clrscr(); printf("Enter degrees to caluclate sin* value"); scanf("%d",&deg); val=sin(deg*(3.1413/180)); printf("sin%d=%d\n",deg,val); printf("Enter degrees to caluclate cos* value"); scanf("%d",&deg); res=cos(deg*(3.1413/180)); printf("cos%d=%d\n",deg,res); res=abs(-5); printf("absolute value of -5=%d\n",res); deg=0; res=exp(deg); printf("exponential value =%d\n",val); val=ceil(4.36); printf("ceil value of 4.36=%d\n",val); val=floor(4.99); printf("floor of (4.99)=%d\n",val); val=log(10); printf("natural log value of log(10)=%d\n",val); val=log10(10); printf("logerthemic value log10(10)=%d\n",val); val =pow(3,4); printf("pow(3,4)=%d\n",val); val =sqrt(9); printf("sqrt of 9=%d\n",val); getch(); } INPUT: Enter the degrees to caluclate Sin* value: 30 OUTPUT: sin 30 = 0.5 INPUT: Enter the degrees to caluclate cos* value: 60 OUTPUT: cos 60 = 0.5 absoulute value of -5 = 5 exponential value of 0 = 1 ceil value of (4.36) = 5 floor value of (4.99) = 4 Natural logerthmic value log10 = 2.302585 logerthmic value of log10(10) = 1.0 power (3,4) = 81 square root (9) = 3

3. Write a C program to convert the given number into years, months and days #include<stdio.h> main() { int nod,year,month,day,temp; clrscr(); printf("enter any number to convert years,months and days"); scanf("%d",&nod); year=nod/365; temp=nod%365; month=temp/30; day=temp%30; printf(" years---%d\n months---%d\n days---%d",year,month,day); getch(); } INPUT: enter any number to convert years,months and days 1001 OUTPUT: years---2 months---9 days---1

4. Write a C program to find largest of three numbers #include<stdio.h> #include<conio.h> main() { int x,y,z,larger; clrscr(); printf("enter any three numbers to find the largest value"); scanf("%d%d%d",&x,&y,&z); if(x>y) larger=x; else larger=y; if(z>larger) larger=z; printf("%d is largest in the given numbers %d %d %d",larger, x,y,z); } INPUT: enter any three number to find the largest value: 11 10 12 OUPUT: 12 is the largest in the given number 11 10 12

5. Write a C program to find largest of three numbers using nested if statement #include<stdio.h> #include<conio.h> main() { int x,y,z; clrscr(); printf("enter any three number to find the largest number"); scanf("%d%d%d",&x,&y,&z); if(x>y) if(x>z) printf("%d is largest",x); else printf("%d is largest",z); else if(y>z) printf("%d is largest",y); else printf("%d is largest",z); } INPUT: enter any three number to find largest number: 10 11 12 OUTPUT: 12 is largest

6.Write a C program to print the factors of given input? #include<stdio.h> #include<conio.h> main() { int n,i; clrscr(); printf("enter any no to find factors"); scanf("%d",&n); for(i=1;i<n;i++) { if(n%i==0) printf("%d is a factor to %d",i,n); } } INPUT: enter any number to find factors: 10 OUTPUT: 1 is a factor to 10 2 is a factor to 10 5 is a factor to 10 10 is a factor to 10

7. Write a C program to calculate sum of individual digits #include<stdio.h> #include<conio.h> main() { int num,temp, sum=0,rem; clrscr(); printf("enter any no to caluclate sum of individual digits"); scanf("%d",&num); temp=num; while (num>0) { rem=num%10; sum=sum+rem; num=num/10; } printf("individual digits sum of %d = %d",temp,sum); } INPUT: enter any no to caluclate sum of individual digits 12345 OUTPUT: individual digit sum of 12345 = 15

8. Write a C program to check whether the given number is palindrome or not? #include<stdio.h> #include<conio.h> main() { int num,rev=0,rem,temp; clrscr(); printf("enter any no to check polyndrome"); scanf("%d",&num); temp=num; while(num>0) { rem=num%10; rev=rev*10+rem; num=num/10; } if(temp==rev) printf("%d is a polyndrome",temp); else printf("%dis not a polyndorme",temp); } INPUT: enter any number to check polindrome: 121 OUTPUT: 121 is a polindrome INPUT: enter any number to check polindrome: 120 OUTPUT: 120 is not a polindrome

9. Write a C program to check whether the given number is perfect or not #include<stdio.h> main() { int i,num,sum=0; clrscr(); printf("enter any number to find perfect number"); scanf("%d",&num); for(i=1;i<num;i++) { if(num%i==0) sum+=i; } if(num==sum) printf("given number %d is a perfect number", num); else printf("given number %d is not a perfect number", num); getch(); } INPUT: Enter any number to find perfect number : 6 OUPUT: given number 6 is a perfect number. INPUT : Enter any number to find perfect number : 10 OUPUT: given number 10 is not a perfect number.

10. Write a C program to print perfect numbers with in the given range #include<stdio.h> main() { int temp,num,i,sum; clrscr(); printf("enter any number to find perfect numbers with in the given range"); scanf("%d",&num); for(temp=1;temp<=num;temp++) { sum=0; for(i=1;i<temp;i++) { if(temp%i==0) sum=sum+i; } if(i==sum) printf("%d is perfect number",i); } getch(); } INPUT: Enter any number to find perfect numbers with in the given range 1000 OUTPUT: 6 is a perfect number 28 is a perfect number 496 is a perfect number

11. Write a C program to check whether the given number is prime or not #include<stdio.h> main() { int i,num,test=0; clrscr(); printf("enter any value to check prime or not"); scanf("%d",&num); for(i=1;i<=num/2;i++) { if(num%i==0) test++; } if(test==0) printf("%d is a prime number", num); else printf("%d is not a prime number", num); getch(); } INPUT: enter any value to check prime or not: 5 OUTPUT: 5 is a prime number INPUT: enter any value to check prime or not: 9 OUTPUT: 9 is not a prime number

12. Write a C program to print list of primes with in the given range #include<stdio.h> main() { int i,num,test=0,temp; clrscr(); printf("enter any value to print list of primes"); scanf("%d",&num); for(temp=1;temp<=num;temp++) { test=0; for(i=2;i<temp;i++) { if(temp%i==0) test=1; } if(test==0) printf("%d is prime number\n",temp); } getch(); } INPUT: enter any value to print list of primes: 10 OUTPUT: 1 is a prime number 2 is a prime number 3 is a prime number 5 is a prime number 7 is a prime number

13. Write a C program to print fibonanci series with in the given range #include<stdio.h> main() { int fn,sn,tn,num,i; clrscr(); printf("enter any number to find fibonanci series"); scanf("%d",&num); fn=0; sn=1; tn=fn+sn; printf("%d\n",fn); printf("%d\n",sn); printf("%d\n",tn); for(i=3;i<=num;i++) { fn=sn; sn=tn; tn=fn+sn; printf("%d\n",tn); } getch(); } INPUT: enter any number to find fibonanci series: 10 OUTPUT: 0 1 1 2 3 5 8

14. Write a C program to find ncr value #include<stdio.h> main() { int i,num,r,nfact,rfact,n_rfact,ncr; clrscr(); printf("enter a value to find n factorial"); scanf("%d",&num); nfact=1; for(i=1;i<=num;i++) nfact=nfact*i; printf("enter a value to find r factorial"); scanf("%d",&r); rfact=1; for(i=1;i<=r;i++) rfact=rfact*i; n_rfact=1; for(i=1;i<=n-r;i++) n_rfact=n_rfact*i; ncr=nfact/(rfact*n_rfact); printf("ncr value--%d",ncr); getch(); } INPUT: enter a value to find n factorial: 4 enter a value to find r factorial: 2 OUTPUT: ncr value: 6

15 . Write a C program to find sum and average of first n natural numbers #include<stdio.h> main() { int num,i,sum=0,avg; clrscr(); printf("enter any number to find sum & avg of first n natural numbers"); scanf("%d",&num); for(i=1;i<=num;i++) { sum=sum+i; } avg=sum/num; printf("sum= %d\n aveage=%d",sum,avg); getch(); } INPUT: enter any number to find sum & avg of first n natural numbers:10 OUTPUT: sum = 55 avg = 5.5

16. Write a C program to find sum and average of any n natural numbers #include<stdio.h> main() { int m,i,num,sum=0,avg; clrscr(); printf("enter how many numbers do you want to find out sum & avg of any n numbers"); scanf("%d",&num); for(i=1;i<=num;i++) { printf("enter the %d number",i); scanf("%d",&m); sum=sum+m; } avg=sum/num; printf("sum= %d\n average=%d",sum,avg); getch(); } INPUT: enter how many numbers do you want to find out sum & avg of any n numbers: 3 enter the 1 number: 15 enter the 2 number: 20 enter the 3 number: 25 OUTPUT: sum = 60 avg = 20

17. Write a C program to find list of odd and even numbers #include<stdio.h> main() { int i,num; clrscr(); printf("enter any number to find out list of odd & even numbers"); scanf("%d",&num); for(i=1;i<=num;i++) { if (i % 2 == 0 ) printf("\n%d is enen no",i); else printf("\n%d is odd no",i); } getch(); } INPUT: enter any number to find out list of odd & even nubmers: 5 OUTPUT: 1 is odd no 2 is even no 3 is odd no 4 is even no 5 is odd no

_______________________________________________________________________ Consumption Units Rate of Charge _______________________________________________________________________ 0-200 Rs. 0.50 per Unit 201-400 Rs. 100 Plus Rs. 0.65 per unit excess of 200 401-600 Rs. 230 plus Rs. 0.80 per unit excess of 400 601 and above Rs. 390 plus Rs. 1.00 per unit excess of 600 _______________________________________________________________________*/ 18. Write a C program to calculate electricity bill for the above slab #include<stdio.h> main() { int nou; float charge; clrscr(); printf("enter number of units to prepare electricity bill"); scanf("%d",&nou); if(nou<=200) charge=(nou*0.50); else if((nou>200)&&(nou<=400)) charge=100+(nou-200)*0.65; else if((nou>400)&&(nou<=600)) charge=230+(nou-400)*0.80; else if(nou>600) charge=390+(nou-600)*1; printf("the bill amount is %5.2f",charge); getch() } INPUT: enter number of units to prepare electricity bill 450 OUTPUT: the bill amount is : 270.00

19. Write a C program to find division of a student #include<stdio.h> main() { int sub1,sub2,sub3,total; float avg; char division[10]; clrscr(); printf("enter marks in subject I"); scanf("%d",&sub1); printf("enter marks in subject II"); scanf("%d",&sub2); printf("enter marks in subject III"); scanf("%d",&sub3); total=sub1+sub2+sub3; avg=total/3; if((sub1<35)||(sub2<35)||(sub3<35)) { strcpy(division,"FAIL"); printf("%s",division); exit(0); } if((avg<=40) { strcpy(division,"THIRD CLASS"); printf("%s",division); } else if(avg<=50) { strcpy(division,"SECOND CLASS"); printf("%s",division); } else if(avg<=60) { strcpy(division,"FIRST CLASS"); printf("%s",division); } else if(avg<=75) { strcpy(division,"DISTINCTION"); printf("%s",division); } getch(); } INPUT: enter marks in subject I : 75 enter marks in subjectII : 85 enter marks in subjectIII : 65 OUTPUT: DISTINCTION

20. Write a C program to print the mathematical table #include<stdio.h> main() { int i,m,n; clrscr(); printf("which table you want\n"); scanf("%d",&n); printf("up to which number you want\n"); scanf("%d",&m); for(i=1;i<=m;i++) printf("\n%d X %d = %d\n",n,i,n*i); getch(); } INPUT: which table you want : 5 up to which number you want : 5 OUTPUT: 5X1=5 5 X 2 = 10 5 X 3 = 15 5 X 4 = 20 5 X 5 = 25

21. Write a program to print mathematical table in the following manner 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 #include<stdio.h> main() { int i,j,m,n; clrscr(); printf("Enter any number to print the mathamatical table \n"); scanf("%d",&n); printf("up to which number you want\n"); scanf("%d",&m); for(i=1;i<=m;i++) printf(" %d",i); for(i=1;i<=n;i++) { printf("\n"); for(j=1;j<=m;j++) printf(" %-2d",j*i); } getch(); } INPUT Enter any number to print the mathematical table 5 Up to which number you want 10 OUTPUT 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50

22. Write a C program to calculate GCD value #include<stdio.h> main() { int n,m,rem=0,x,y; clrscr(); printf("enter any two numbers to calculate GCD"); scanf("%d%d",&m,&n); x=m; y=n; rem=m%n; while(rem!=0) { m=n; n=rem; rem=m%n; } printf(" g.c.d of the given numbers%d,%d=%d",x,y,rem); getch(); } INPUT: enter any two numbers to calculate GCD : 5 , 7 OUTPUT: g.c.d. of the given numbers 5,7 = 1

23. Write a C program to print the given number in words #include<stdio.h> main() { int num,rem,rn=0,op; clrscr(); printf("enter any number"); scanf("%d",&num); while(num>0) { rem=num%10; rn=rn*10+rem; num=num%10; } while(rn>0) { op=rn%10; switch(op) { case 1: printf(" ONE"); break; case 2: printf(" TWO"); break; case 3: printf(" THREE"); break; case 4: printf(" FOUR"); break; case 5: printf(" FIVE"); break; case 6: printf(" SIX"); break; case 7: printf(" SEVEN"); break; case 8: printf(" EIGHT"); break; case 9: printf(" NINE"); break; default: printf(" ZERO"); break; } rn=rn/10; } } INPUT: enter any number : 789 OUTPUT: SEVEN EIGHT NINE

24.Write a C program to find the roots of a quadratic equation #include<math.h> main() { int a,b,c; float disc,root1,root2,d1; clrscr(); printf("enter the value of a,b,c for quadratic equation"); scanf("%d%d%d",&a,&b,&c); disc=(b*b-4*a*c); d1=sqrt(disc); if(disc>0) { printf("roots are real \n"); root1=(-b+d1)/2*a; root2=(-b-d1)/2*a; printf("root1=%f root2=%f",root1,root2); } else if(disc==0) { printf("roots are equal"); root1=(-b+d1)/2*a; root2=(-b-d1)/2*a; printf("root1=%f root2=%f",root1,root2); } else if(disc<0) printf("roots are imaginary"); getch(); } INPUT: enter the values of abc for quadratic equation: 1 2 1 OUTPUT: roots are equal root1 = -1 root 2 = -1

25. Write a C program to find largest element from the given numbers #include<stdio.h> main() { int num,large=32767,i,temp; clrscr(); printf("\nenter how many numbers do you want to check to find out the largest number"); scanf("%d",&num); for(i=0;i<num;i++) { printf("\n enter the %d number",i+1); scanf("%d",&temp); if(temp<large) large=temp; } printf("\n largest number among the given numbers=%d",large); } INPUT: enter how many numbers do you want to check to find out the largest number: 5 enter the 1 number : 10 enter the 2 number: 5 enter the 3 number: 15 enter the 4 number: 2 enter the 5 number: 20 OUTPUT: largest number among the given numbers= 20

/* 26. Write a C program to find second largest element from the given numbers*/ #include<stdio.h> main() { int num,large=32767,slarge=-32767,i,temp; clrscr(); printf("\nenter how many numbers do you want to check to find out the Second largest number"); scanf("%d",&num); for(i=0;i<num;i++) { printf("\n enter the %d number",i+1); scanf("%d",&temp); if(temp>large) { slarge=large; large=temp; } else if((temp<large)&&(temp>slarge)) slarge=temp; } printf("\n second largest number among the given numbers=%d",slarge); } INPUT: enter how many numbers do you want to check to find out the Second largest number:5 enter the 1 number : 10 enter the 2 number: 5 enter the 3 number: 15 enter the 4 number: 2 enter the 5 number: 20 OUTPUT: Second largest number among the given numbers = 15

27. Write a C program to find smallest element from the given numbers #include<stdio.h> main() { int num,small=32767,i,temp; clrscr(); printf("\n enter how many number to you want to check to find out the smallest number"); scanf("%d",&num); for(i=0;i<num;i++) { printf("\n enter the %d number",i+1); scanf("%d",&temp); if(temp<small) small=temp; } printf("\n smallest number among the given numbers=%d",small); } INPUT: enter how many number to you want to check to find out the smallest number enter the 1 number : 10 enter the 2 number: 5 enter the 3 number: -15 enter the 4 number: -2 enter the 5 number: -20 OUTPUT: smallest number among the given numbers= -20

28. Write a C program to find second smallest element from the given numbers #include<stdio.h> main() { int num,small=32767,ssmall=32767,i,n; clrscr(); printf("\nenter how many number to you want to check to find out the second smallest number"); scanf("%d",&num); for(i=0;i<num;i++) { printf("\n enter the %d number",i+1); scanf("%d",&n); if(n<small) { ssmall=small; small=n; } else if((n>small)&&(n<ssmall)) ssmall=n; } printf("\n second smallest number among the given numbers=%d",ssmall); } INPUT: enter how many number to you want to check to find out the second smallest number:5 enter the 1 number : 10 enter the 2 number: 5 enter the 3 number: -15 enter the 4 number: -2 enter the 5 number: -20 OUTPUT: second smallest number among the given numbers= -15

29. Write a C program to check whether the given number is strong or not #include<stdio.h> main() { int num,rem,i,strong=0,fact=1,temp; clrscr(); printf("Enter any number to check whether it is strong or not"); scanf("%d",&num); temp=num; while(num>0) { rem=num%10; fact=1; for(i=1;i<=rem;i++) fact=fact*i; strong=strong+fact; num=num/10; } if(temp==strong) printf("%d is a strong number",temp); else printf("%d is not a strong number",temp); getch(); } INPUT: Enter any number to check whether it is strong or not OUTPUT: 153 is a strong number INPUT: Enter any number to check whether it is strong or not OUTPUT: 123 is not a strong number

/* 30. Write a C program to list the strong numbers in the given range*/ #include<stdio.h> main() { long int num,rem,i,j,strong=0,fact=1,temp; clrscr(); printf("Enter any number to find list of strong numbers"); scanf("%ld",&num); for(j=1;j<num;j++) { strong=0; temp=j; while(temp>0) { rem=temp%10; fact=1; for(i=1;i<=rem;i++) fact=fact*i; strong=strong+fact; temp=temp/10; } if(j==strong) printf("\n%ld is a strong number",j); } getch(); } INPUT: Enter any number to find list of strong numbers: 100000 OUTPUT: 1 is a strong number 2 is a strong number 145 is a strong number 40585 is a strong number

31. Write a C program to check whether the given number is amstrong ot not #include<stdio.h> main() { int num,rem,amst=0,temp; clrscr(); printf("\nenter any number to find amstrong or not"); scanf("%d",&num); temp=num; while(temp>0) { rem=temp%10; amst=amst+rem*rem*rem; temp=temp/10; } if(num==amst) printf("%d is a Amstrong number ",num); else printf("%d is not a Amstrong number",num); getch(); } INPUT: enter any number to find amstrong or not 153 OUTPUT: 153 is amstrong number INPUT: enter any number to find amstrong or not 123 OUTPUT: 123 is not a amstrong number

/*32.Write a C program to list amstrong number with in the given range*/ #include<stdio.h> main() { int num,i,n,r,amst=0,temp; clrscr(); printf("enter any number to find list of amstrong numbers"); scanf("%d",&num); for(i=1;i<=num;i++) { temp=i; amst=0; while(temp>0) { r=temp%10; amst=amst+r*r*r; temp=temp/10; } if(i==amst) printf("\n%d is a Amstrong number",i); } getch(); } INPUT: enter any number to find list of amstrong numbers: 1000 OUTPUT: 1 is a amstrong number 153 is a amstrong number 370 is a amstrong number 371 is a amstrong number 407 is a amstrong number

/*33. Write a C program to find whether the given number is magic or not*/ #include<stdio.h> #include<math.h> main() { int sqr,num,rem,rev=0,magic=0,sroot; clrscr(); printf("enter any number to find magic or not"); scanf("%d",&num); sqr=num*num; while(sqr>0) { rem=sqr%10; rev=rev*10+rem; sqr=sqr/10; } sroot=sqrt(rev); rev=0; while(sroot>0) { rem=sroot%10; rev=rev*10+rem; sroot=sroot/10; } if(num==rev) printf("%d is a magic number",num); else printf("%d is not a magic number",num); getch(); } INPUT: Enter any number to find magic or not 12 OUTPUT: 12 is a magic number INPUT: Enter any number to find magic or not 14 OUTPUT: 14 is not a magic number

34. Write a C program to print the magic numbers with in the given range #include<stdio.h> #include<math.h> main() { int sqr,num,rem,rev=0,magic=0,sroot,i; clrscr(); printf("Enter any number to find list of magic numbers"); scanf("%d",&num); for(i=0;i<=num;i++) { sqr=i*i; rev=0; while(sqr>0) { rem=sqr%10; rev=rev*10+rem; sqr=sqr/10; } sroot=sqrt(rev); rev=0; while(sroot>0) { rem=sroot%10; rev=rev*10+rem; sroot=sroot/10; } if(i==rev) printf("\n%d is a magic number",i); } getch(); } INPUT: Enter any number to find list of magic numbers 100 OUTPUT: 1 is a magic number 11 is a magic number 12 is a magic number 13 is a magic number

/*35. Write a C program to find largest element in the list */ #include<stdio.h> main() { int a[10],i,large=-32767,num; clrscr(); printf("enter how many elements in the list"); scanf("%d",&num); for(i=0;i<num;i++) { printf("enter the %d element in the list",i+1); scanf("%d",&a[i]); } large=a[0]; for(i=0;i<num;i++) { if((a[i])>large) large=a[i]; } printf("largest element in the list is %d",large); getch(); } INPUT: enter how many elements in the given list: 5 enter the 1 elelment in the list: 10 enter the 2 elelment in the list: 5 enter the 3 elelment in the list: 15 enter the 4 elelment in the list: 2 enter the 5 elelment in the list: 20 OUTPUT: largest element in the list is 20

list*/ #include<stdio.h> main() { int a[10],i,num,slarge,large; clrscr(); printf("enter how many elements in the list"); scanf("%d",&num); for(i=0;i<num;i++) { printf("enter the %d element in the list",i+1); scanf("%d",&a[i]); } large=a[1],slarge=0; for(i=0;i<num;i++) { if((a[i])>large) large=a[i]; } for(i=0;i<num;i++) { if((a[i]>slarge)&&(a[i]!=large)) slarge=a[i]; } printf("largest elementis %d second largest element is %d",large,slarge); getch(); } INPUT: enter how many elements in the given list: 5 enter the 1 elelment in the list: 10 enter the 2 elelment in the list: 5 enter the 3 elelment in the list: 15 enter the 4 elelment in the list: 2 enter the 5 elelment in the list: 20 OUTPUT: largest element is 20 second largest elelment is 15 #include<stdio.h> main() { int a[10],i,num,small; clrscr(); printf("enter how many elements in the list"); scanf("%d",&num); for(i=1;i<=num;i++) { printf("enter the %d element in the list",i); scanf("%d",&a[i]); } small=a[0]; for(i=0;i<num;i++) {

if((a[i])<small) small=a[i]; } printf("smallest element in the given list is %d",small); getch(); } INPUT: enter how many elements in the given list: 5 enter the 1 elelment in the list: 10 enter the 2 elelment in the list: 5 enter the 3 elelment in the list: 15 enter the 4 elelment in the list: 2 enter the 5 elelment in the list: 20 OUTPUT: Smallest element in the given list is 20 element in the given list*/ #include<stdio.h> main() { int i,num,a[10],small,ssmall,large; clrscr(); printf("enter how many elements in the list"); scanf("%d",&num); for(i=1;i<=num;i++) { printf("enter teh %d element in the list",i); scanf("%d",&a[i]); } large=a[1]; for(i=2;i<=num;i++) { if(a[i]>large) large=a[i]; } small=a[1]; for(i=2;i<=num;i++) { if(a[i]<small) small=a[i]; } printf("smallest elelment is %d",small); ssmall=large; for(i=2;i<=num;i++) { if((a[i]<ssmall)&&(a[i]!=small)) ssmall=a[i]; } printf("\tsecond smallest element is %d",ssmall); getch(); } INPUT: enter how many elements in the given list: 5

enter the 1 elelment in the list: 10 enter the 2 elelment in the list: 5 enter the 3 elelment in the list: 15 enter the 4 elelment in the list: 2 enter the 5 elelment in the list: 20 OUTPUT: smallest element is 2 second smallest elelment is 5 /*39. Write a C program to serch an element using the linear serch*/ #include<stdio.h> main() { int a[10],i,num,key,flag=0; clrscr(); printf("enter how many elements in the list"); scanf("%d",&num); printf("enter the elements in the list for linear search"); for(i=0;i<num;i++) { printf("enter the %d element in the list",i+1); scanf("%d",&a[i]); } printf("enter the serch element in the list"); scanf("%d",&key); for(i=0;i<num;i++) { if(a[i]==key) { flag=1; break; } } if(flag==1) printf("serch is sucessful"); else printf("%d is not found in the list",key); getch(); } INPUT: enter how many elements in the given list: 5 enter the elements in the list for linear search enter the 1 elelment in the list: 10 enter the 2 elelment in the list: 5 enter the 3 elelment in the list: 15 enter the 4 elelment in the list: 2 enter the 5 elelment in the list: 20 enter the search element: 15 OUTPUT: search is succesful #include<stdio.h> main() { int a[10],i,num,loc=1,key,low,high,mid; clrscr();

printf("enter how many elements in the list"); scanf("%d",&num); printf("enter the elements in the list for binary search"); for(i=1;i<=num;i++) { printf("enter the %d element in the given list",i+1); scanf("%d",&a[i]); } printf("enter the serch element in the list"); scanf("%d",&key); low=1; high=num; mid=(low+high)/2; while((a[mid]!=key)&&(low!=high)) { if(a[mid]>key) low=mid+1; else high=mid-1; mid=(low+high)/2; if(a[mid]==key) loc=mid; else loc=0; } if(loc==0) printf("serch is not sucessful"); else printf("serch is sucessful %d is find in %d location",key,loc); getch(); } INPUT: enter how many elements in the given list: 5 enter the elements in the list for binary search enter the 1 elelment in the list: 10 enter the 2 elelment in the list: 5 enter the 3 elelment in the list: 15 enter the 4 elelment in the list: 2 enter the 5 elelment in the list: 20 enter the search element: 15 OUTPUT: search is succesful main() { int a[10],temp,i,j,num; clrscr(); printf("enter how many elements in the list"); scanf("%d",&num); printf("enter the elements in the list to sort in assending order"); for(i=1;i<=num;i++) { printf("enter the %d element in the given list",i);

scanf("%d",&a[i]); } temp=a[i]; for(i=1;i<=num-1;i++) { for(j=i+1;j<=num;j++) { if(a[i]>a[j]) { temp=a[j]; a[j]=a[i]; a[i]=temp; } } } printf("the sort list is"); for(i=1;i<=num;i++) printf("%d ",a[i]); getch(); } INPUT: enter how many elements in the given list: 5 enter the elements in the list to sort in assending order enter the 1 elelment in the list: 1 enter the 2 elelment in the list: 6 enter the 3 elelment in the list: 7 enter the 4 elelment in the list: 2 enter the 5 elelment in the list: 8 OUTPUT: the sorted list is : 1 2 6 7 8 main() { int a[10],temp,i,j,num; clrscr(); printf("enter how many elements in the list"); scanf("%d",&n); printf("enter the elements in the list to sort in dessending order"); for(i=1;i<=num;i++) { printf("enter the %d element in the list",i); scanf("%d",&a[i]); } temp=a[i]; for(i=1;i<=num-1;i++) { for(j=i+1;j<=num;j++) { if(a[i]<a[j]) { temp=a[j]; a[j]=a[i]; a[i]=temp;

} } } printf("the sort list is"); for(i=1;i<=num;i++) printf("%d ",a[i]); getch(); } INPUT: enter how many elements in the given list: 5 enter the elements in the list to sort in dessending order enter the 1 elelment in the list: 1 enter the 2 elelment in the list: 6 enter the 3 elelment in the list: 7 enter the 4 elelment in the list: 2 enter the 5 elelment in the list: 8 OUTPUT: the sorted list is : 8 7 6 2 1 order using bubble sort*/ #include <stdio.h> main() { int a[10],i,j,num,temp; clrscr(); printf("enter how many elements in the list"); scanf("%d",&num); printf("enter the elements in the list to sort the bubble sort"); for(i=0;i<num;i++) { printf("enter the %d element in the given list",i+1); scanf("%d",&a[i]); } for(i=0;i<num;i++) { for(j=0;j<num-1;j++) { if(a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } printf("the sorted list is"); for(i=0;i<num;i++) printf("%d\n",a[i]); getch(); } INPUT: enter how many elements in the given list: 5 enter the elements in the list to sort bubble sort enter the 1 elelment in the list: 1

enter the 2 elelment in the list: 6 enter the 3 elelment in the list: 7 enter the 4 elelment in the list: 2 enter the 5 elelment in the list: 8 OUTPUT: the sorted list is : 1 2 6 7 8 #include<stdio.h> #include<math.h> main() { int a[10][10],b[10][10],c[10][10],i,j,row,col; clrscr(); printf("enter the dimention of the matrix"); scanf("%d%d",&row,&col); printf("enter the elements of the first matrix \n"); for(i=0;i<row;i++) { for(j=0;j<col;j++) { printf("enter the %d row %d coloum elements of matrix",i,j); scanf("%d",&a[i][j]); } } printf("enter the elements of the second matrix \n"); for(i=0;i<row;i++) { for(j=0;j<col;j++) { printf("enter the %drow %dcoloum elements of the matrix",i,j); scanf("%d",&b[i][j]); } } for(i=0;i<row;i++) { for(j=0;j<col;j++) c[i][j]=a[i][j]+b[i][j]; d[i][j]=a[i][j]-b[i][j]; } printf("the resultent matrix is \n"); for(i=0;i<row;i++) { for(j=0;j<col;j++) printf("%4d",c[i][j]); } printf("\n"); for(i=0;i<row;i++) { for(j=0;j<col;j++) { printf("%4d",d[i][j]); } printf("\n");

} getch() } INPUT: enter the dimansion of the matrix: 2x2 enter the elements of the first matrix: enter the 1 row 1 column element in the matrix: 1 enter the 1 row 2 column element in the matrix: 2 enter the 2 row 1 column element in the matrix: 3 enter the 2 row 2 column element in the matrix: 4 enter the elements of the second matrix: enter the 1 row 1 column element in the matrix: 5 enter the 1 row 2 column element in the matrix: 6 enter the 2 row 1 column element in the matrix: 7 enter the 2 row 2 column element in the matrix: 8 OUTPUT: The resultant matrix is : 6 8 10 12 -4 -4 -4 -4 /* 45. Write a C program for multiplication of 2 m x n matrices*/ #include<stdio.h> #include<math.h> main() { int a[10][10],b[10][10],i,j,row1,col1,row2,col2,k,c[10][10]; clrscr(); printf("enter the dimention of the matrix"); scanf("%d%d",&row1,&col1); printf("enter the elements of the first matrix \n"); for(i=0;i<row1;i++) { for(j=0;j<col1;j++) { printf("enter the %d row %d coloum elements of the matrix",i,j); scanf("%d",&a[i][j]); } } printf("enter the elements in second matrix "); scanf("%d%d",&row2,&col2); printf(" enter the elements of second matrix \n"); for(i=0;i<row2;i++) { for(j=0;j<col2;j++) { printf("enter the %drow %dcoloum elements of the matrix",i,j); scanf("%d",&b[i][j]); } } if(col1==row2) { printf("the resultent matrix is \n"); for(i=0;i<row1;i++)

{ for(j=0;j<col2;j++) { c[i][j]=0; for(k=0;k<col1;k++) { c[i][j]=c[i][j]+a[i][k]*b[k][j]; } }} for(i=0;i<row1;i++) { for(j=0;j<col1;j++) { printf("%4d",c[i][j]); } printf("\n"); }} else printf("the matrix are not same order"); getch(); } INPUT: enter the dimension of the martix: 2 2 enter the elements of the first matrix: enter the 1 row 1 column element in the matrix: 1 enter the 1 row 2 column element in the matrix: 2 enter the 2 row 1 column element in the matrix: 3 enter the 2 row 2 column element in the matrix: 4 enter the elements of the second matrix: enter the 1 row 1 column element in the matrix: 5 enter the 1 row 2 column element in the matrix: 6 enter the 2 row 1 column element in the matrix: 7 enter the 2 row 2 column element in the matrix: 8 OUTPUT: the resultant matrix is: 19 22 43 50 #include<stdio.h> main() { int i,j,col,row,sum=0,a[10][10]; clrscr(); printf("enter the dimension of the matrix"); scanf("%d%d",&row,&col); for(i=0;i<row;i++) { for(j=0;j<col;j++) { printf("enter %d row and%d column of the matrix",i,j); scanf("%d",&a[i][j]); }} for(i=0;i<row;i++) sum=sum+a[i][i];

printf("trace of the given matrix is %d",sum); getch(); } INPUT: enter the dimension of the martix: 2 2 enter the elements in the matrix: enter the 1 row 1 column element in the matrix: 1 enter the 1 row 2 column element in the matrix: 2 enter the 2 row 1 column element in the matrix: 3 enter the 2 row 2 column element in the matrix: 4 OUTPUT: the trace of the given matrix is 5: #include<stdio.h> main() { int i,j,row,col,a[10][10]; clrscr(); printf("enter the dimension of the matrix "); scanf("%d%d",&row,&col); for(i=0;i<row;i++) { for(j=0;j<col;j++) { printf("enter %d row and %d column elements",i,j); scanf("%d",&a[i][j]); } } printf("\ngiven matrix\n"); for(i=0;i<row;i++) { for(j=0;j<col;j++) { printf("%4d",a[i][j]); printf("\n"); } } printf("\ntranspose of given matrix\n"); for(i=0;i<row;i++) { for(j=0;j<col;j++) { printf("%4d",a[j][i]); } printf("\n"); } getch(); } INPUT: enter the dimension of the martix: 2 2 enter the elements in the matrix: enter the 1 row 1 column element in the matrix: 1

enter the 1 row 2 column element in the matrix: 2 enter the 2 row 1 column element in the matrix: 3 enter the 2 row 2 column element in the matrix: 4 OUTPUT: the given matrix is 12 34 the transpose of the given matrix is: 13 24 /*48.Write a C program to find row sum and coloum sum of the given matrix*/ #include <stdio.h> main() { int a[4][4],i,j,row,col; clrscr(); printf("enter the dimension of the matrix"); scanf("%d%d",&row,&col); printf("enter the elements in the list \n"); for(i=1;i<=row;i++) { for(j=1;j<=col;j++) { printf("enter %d row and %d column elements",i,j); scanf("%d",&a[i][j]); } } for(i=1;i<=row;i++) { a[i][col+1]=0; for(j=1;j<=col;j++) { a[i][col+1]=a[i][col+1]+a[i][j]; } } for(i=1;i<=col;i++) { a[row+1][i]=0; for(j=1;j<=row;j++) { a[row+1][i]=a[row+1][i]+a[j][i]; } } printf("the elements in the list and row sum and coloumn sum is\n"); for(i=1;i<=row+1;i++) { for(j=1;j<=col+1;j++) { if((i==row+1) && (j==col+1)) break; printf("%4d",a[i][j]);

} printf("\n"); } getch(); } INPUT: enter the dimension of the martix: 2 2 enter the elements in the matrix: enter the 1 row 1 column element in the matrix: 1 enter the 1 row 2 column element in the matrix: 2 enter the 2 row 1 column element in the matrix: 3 enter the 2 row 2 column element in the matrix: 4 OUTPUT: the resultant matrix is: 123 459 57 /*49. Write a C program to print upper triangle elements in the given matrix*/ #include <stdio.h> main() { int a[10][10],i,j,row,col; clrscr(); printf("enter the dimension of the matrix"); scanf("%d%d",&row,&col); printf("enter the elements in the matrix"); for(i=1;i<=row;i++) { for(j=1;j<=col;j++) { printf("enter %d row and %d column elements",i,j); scanf("%d",&a[i][j]); } } printf("the upper triangular elements of the given matrix is"); for(i=1;i<=row;i++) { for(j=1;j<=col;j++) { if(i<=j) printf("%u ",a[i][j]); if(i>j) printf(" "); } printf("\n"); } getch(); } INPUT: enter the dimension of the martix: 2 2 enter the elements in the matrix: enter the 1 row 1 column element in the matrix: 1

enter the 1 row 2 column element in the matrix: 2 enter the 2 row 1 column element in the matrix: 3 enter the 2 row 2 column element in the matrix: 4 OUTPUT: the upper triangular elements of the given matrix is 12 5 /*50. Write a C program to find upper diagnal sum of given matrices*/ #include <stdio.h> main() { int a[10][10],i,j,row,col,sum=0; clrscr(); printf("enter the dimension of the matrix"); scanf("%d%d",&row,&col); printf("enter the elements in the matrix"); for(i=1;i<=row;i++) { for(j=1;j<=col;j++) { printf("enter %d row and %d column elements",i,j); scanf("%d",&a[i][j]); } } printf("sum of the upper diagonal elements "); for(i=1;i<=row;i++) { for(j=1;j<=col;j++) { if(i<j) sum=sum+a[i][j]; } } printf("sum=%d",sum); getch(); } INPUT: enter the dimension of the martix: 2 2 enter the elements of the first matrix: enter the 1 row 1 column element in the matrix: 1 enter the 1 row 2 column element in the matrix: 2 enter the 2 row 1 column element in the matrix: 3 enter the 2 row 2 column element in the matrix: 4 OUTPUT: the upper diagnal sum of the given matrix is : 2 #include<string.h> main() { int length; char s[10]; clrscr(); printf("enter any string to find string length"); scanf("%s",s);

length=strlen(s); printf("length of the given string =%d",length); getch(); } /* Write a C program to find length of the given string with out using liabrary functions */ #include<stdio.h> #include<string.h> main() { int i,length=0; char st[20]; printf("\nenter any string to find string length\n"); gets(st); for(i=0;st[i]!='\0';i++) length++; printf("Length of the string %s = %d ",st,length); } INPUT: enter any string to find string length: VRS & YRN COLLEGE. OUPUT: length of the given string: 18 /*52. Write a C program to find the string concotination*/ #include<stdio.h> main() { char s[10],r[10]; clrscr(); printf("enter the first string for concotination"); scanf("%s",s); printf("enter the second string"); scanf("%s",r); strcat(s,r); printf("after string concatination concotinated string is %s",s); getch(); } /* Write a C program to concatinating one string to another string with out using liabrary functions */ #include<stdio.h> #include<string.h> main() { int i,length=0; char st1[20],st2[20]; printf("\nenter First string for concatinating\n"); gets(st1); printf("\nenter Second string for concatinating\n"); gets(st2); for(i=0;st1[i]!='\0';i++) length++; for(i=0;st2[i]!='\0';i++) { st1[length]=st2[i];

length++; } st1[length]='\0'; printf("After concatinating concotinated string is %s ",st1); } INPUT: enter the first string for concotination: vrs enter the second string: yrn OUTPUT: after string concotination concotinated string is : vrsyrn /*53. Write a C program to compare the given string*/ #include<stdio.h> #include<string.h> main() { int res; char s1[10],s2[10]; clrscr(); printf("enter first string for string comparision"); gets(s1); printf("enter second string for string comparision"); gets(s2); res=strcmp(s1,s2); if(res==0) printf("both strings are equal"); else if(res>0) printf("the first string>second string"); else if(res<0) printf("the second string>first string"); getch(); } /* Write a C program to compare one string to another string with out using liabrary functions */ #include<stdio.h> #include<string.h> main() { int i,flag=1; char st1[20],st2[20]; clrscr(); printf("\nenter First string for comparision\n"); gets(st1); printf("\nenter Second string for comparision\n"); gets(st2); for(i=0;st1[i]&&st2[i]!='\0';i++) { if(st1[i]==st2[i]) continue; else flag=0; if(st1[i]<st2[i])

printf("%s < %s " ,st1,st2); else if(st1[i]>st2[i]) printf("%s > %s " ,st1,st2); } if(flag) printf("Both strings are eaqual\t %s = %s ",st1 ,st2); } INPUT: enter the first string for comparision : abc enter the second string for comparision: abd OUTPUT: the second string > first string. #include<stdio.h> main() { char st1[10],st2[10]; clrscr(); printf("enter the first string for copying "); scanf("%s",st1); printf("enter the second string for copying"); scanf("%s",st2); strcpy(st1,st2); printf("after string copy first string = %s second string = %s",st1,st2); getch(); } /* Write a C program to copying one string to another string with out using liabrary functions */ #include<stdio.h> #include<string.h> main() { int i; char st1[20],st2[20]; printf("\nenter First string for copying\n"); gets(st1); printf("\nenter Second string for copying\n"); gets(st2); for(i=0;st2[i]!='\0';i++) st1[i]=st2[i]; st1[i]='\0'; printf("After string copy first string is %s, second string = %s",st1,st2); } INPUT: enter the first string for copying : vrs enter the second string for copying: yrn OUTPUT: after string copy first string = yrn second string = yrn /*55. Write a C program to given string is PALINDROME or Not*/ #include<stdio.h> main() { int n,i,j,res;

char s[10],r[10]; clrscr(); printf("\n \nenter any string to check string polindrome or not"); scanf("%s",s); n=strlen(s); for(i=n-1,j=0;i>=0;i--,j++) r[j]=s[i]; r[j]='\0'; res=strcmp(s,r); if(res==0) printf("\n \n%s is a palindrome",s); else printf("\n \n%s is not a palindrome",s); getch(); } INPUT: enter any string to check string polindrome or not: liril OUTPUT: liril is a polindrome INPUT: enter any string to check string polindrome or not: afdkl OUTPUT: afdkl is not a polindrome. #include<stdio.h> main() { int num,i,j,k; char names[20][10],temp[10],ch; clrscr(); num=0; printf("enter the names for one line \n"); scanf("%s",names); while((strcmp(names[n],"end")>0)) { num++; scanf("%s",names[num]); } printf("\n"); for(i=0;i<num;i++) printf("%10s",names[i]); printf("\n\n"); printf("total names=%d\n",num); for(i=0;i<=num-1;i++) for(j=j+1;j<=num;j++) { if((strcmp(names[i],names[j])>0)) { strcmp(temp,names[i]); strcmp(names[i],names[j]); strcmp(names[j],temp); } } printf("the sorting names are \n"); for(i=0;i<num;i++) printf("%10s",names[i]);

printf("\n"); getch(); } INPUT: enter the names for one line rama raja krishna sunitha adiseshu kiran vrs yrn suneel bsc END OUTPUT adiseshu bsc kiran krishna raja rama suneel sunitha vrs yrn recursive*/ main() { int num,f,fact(int); clrscr(); printf("enter any value to find factorial"); scanf("%d",&num); f=fact(num); printf("factorial of the given no is %d",f); getch(); } fact(x) { int f=1; if(x==0) return(1); else f=x*fact(x-1); return(f); } INPUT: enter any value to find factorial 5 OUTPUT: factorial of the given no is 120 #include<stdio.h> main() { int num; clrscr(); printf("num of turms to be generation"); scanf("%d",&num); printf("fibonanci sequence up to %d turms is \n",num); fib(num); getch(); } fib(num) int num; { static int f1=0,f2=0; int temp; if(num<2) { f1=0;

f2=0; } else { fib(num-1); temp=f2; f2=f1+f2; f1=temp; printf("%5d",f1); return; } } INPUT: num of turms to be generation 5 OUTPUT: 0 1 1 2 3 /*59. Write a C program to reverse the given string using recursion*/ #include<stdio.h> #include<string.h> main() { int length; void rev(); char s[10]; clrscr(); printf("enter any string to reverce"); gets(s); length=strlen(s)-1; rev(s,length); } void rev(length,s) int length; char s[20]; { if(length==0) putch(s[length]); rev(--length,s); return; } INPUT: enter any string to reverce vrs OUTPUT: srv functions*/ #include<stdio.h> main() { int m,n,gcd1; clrscr(); printf("enter m,n values"); scanf("%d%d",&m,&n); gcd1=gcd(m,n); printf("gcd of given two num=%d",gcd1);

getch(); } gcd(x,y) int x,y; { int rem; if(x%y==0) return y; rem=x%y; while(rem!=0) { x=y; y=rem; rem=x%y; } return y; } INPUT: enter m,n values 5, 7 OUTPUT: gcd of given two num= 1 functions*/ #include<stdio.h> main() { int m,n,gcd1,product,lcm; clrscr(); printf("enter m,n values"); scanf("%d%d",&m,&n); product=m*n; gcd1=gcd(m,n); lcm=product/gcd1; printf("lcm of given two num=%d",lcm); getch(); } gcd(x,y) int x,y; { int rem; if(x%y==0) return y; rem=x%y; while(rem!=0) { x=y; y=rem; rem=x%y; } return y; } INPUT: enter m,n values 5, 7

OUTPUT: lcm of given two num= 35 /*62 . write a C program to reverse the given numbers using functions*/ #include<stdio.h> main() { int num,rev; clrscr(); printf("enter any no to reverce num"); scanf("%d",&num); rev=reverce(num); printf("reverce of the given num%d=%d",num,rev); getch(); } reverce(x) int x; { int rem,s=0; while(x>0) { rem=x%10; s=s*10+rem; x=x/10; } return s; } INPUT: enter any no to reverce num 625 OUTPUT: reverce of the given num 526 #include<stdio.h> main() { int a=10,b=20; clrscr(); swapr(a,b); printf("a=%d\n",a); printf("b=%d\n",b); } swapr(x,y) { int t; t=x; x=y; y=t; printf("x=%d\n",x); printf("y=%d\n",y); getch(); } OUTPUT: a=10 b=20 x=20 y=10.ls1 */ #include<stdio.h>

main() { int a=10,b=20; clrscr(); swapr(&a,&b); printf("a=%d\n",a); printf("b=%d\n",b); } swapr(x,y) int *x,*y; { int t; t=*x; *x=*y; *y=t; printf("x=%d\n",*x); printf("y=%d\n",*y); getch(); } OUTPUT: a=20 b=10 x=20 y=10 #include<stdio.h> main() { int m=1000; function2(); printf("%d\n",m); } function1() { int m=1000; printf("%d\n",m); } function2() { int m=100; function1(); printf("%d\n",m); } output: 10 100 1000. #include<stdio.h> main() { x=10; printf("%d\n",x); printf("%d\n",fun1()); printf("%d\n",fun2()); printf("%d\n",fun3()); }

fun1() { x=x+10; return(x); } fun2() { int x; x=1; return(x); } fun3() { x=x+10; return(x); } output: x=10 x=20 x=1 x=30. /*67. Write a C program to illustrate static storage class*/ #include<stdio.h> main() { int i; for(i=0;i<=3;i++) stat(); } stat() { static int x=0; x=x+1; printf("x=%d\n",x); } output: x=1 x=2 x=3. using C prototype.*/ #include<stdio.h> main() { int num;float a,b; float square( float a); clrscr(); printf("enter any no to find square of the given number"); scanf("%d",&num); b= square(a); printf("square of the %f=%f",a,b); getch();

} float square(x) float x; { float y; y=x*x; return y; } INPUT: enter any no to find square 1.5 OUTPUT: square of the 1.5=3.75 main() { int radius; float area,perimeter; clrscr(); printf("enter the radius of a circle"); scanf("%d",&radius); areaperi(radius,&area,&perimeter); printf("area=%f \n",area); printf("perimeter=%f \n",perimeter); getch(); } areaperi(r,a,p) int r; float *a,*p; { *a=3.14*r*r; *p=2*3.14*r; } INPUT: enter the radius of a circle 7 OUTPUT: area = 154 perimeter=44 functions*/ #include<stdio.h> main() { int num,f; clrscr(); printf("enter any no to find the factorial"); scanf("%d",&num); f=fact(num); printf("factorial of the given num %d is %d\n",num,f ); getch(); } fact(x) int x; { int f=1,i; for(i=1;i<=x;i++) f=f*i; return (f);

} INPUT: enter any no to find the factorial 5 OUTPUT: factorial of the given num 5 is 120 #include<stdio.h> main(argc,argv) int argc; char *argv[]; { int i; for(i=0;i<argc;i++) printf("%s\n",argv[i]); } INPUT: cat dog pot abc def sd fg hj output: cat dog pot abc def sd fg hj display them on VDU*/ main() { struct book { char name[10]; float price; int pages; }; static struct book b1={"Let Us C, 150,520"}; printf("Book name =%s\n", b1.name); printf("Book price=%f\n", b1.price); printf("Book pages=%d\n", b1.pages); } OUTPUT: Book Name = Let us C Book Price= 150 Book Pages= 520 copying*/ #include<stdio.h> main() { struct emp { char name[10]; int age; float salary; }; static struct emp e1= {"Sanjay", 30,5500}; struct emp e2,e3; /*piece meal copying*/ strcpy(e2.name,e1.name); e2.age=e1.age; e2.salary=e1.salary; /*copying all elements at one go*/ e3=e2; printf("%s%d%f\n", e1.name, e1.age, e1.salary); printf("%s%d%f\n", e2.name, e2.age, e2.salary);

printf("%s%d%f\n", e3.name, e3.age, e3.salary); } OUTPUT: Sanjay, 30 5500 Sanjay, 30 5500 Sanjay, 30 5500 array of structure*/ #include<stdio.h> main() { strct book { char name[10]; float price; int pages; }; struct book b[10]; int i; for(i=0;i<=9;i++) { printf("\n Enter name, price, and pages"); scanf("%s%f%d", &b[i].name, &b[i].price, &b[i].pages); } for(i=0;i<=9;i++); printf("%s%f%d", b[i].name, b[i].price, b[i].pages); } OUTPUT: #include<stdio.h> struct book { char name[25]; char author[25]; int callno; }; main() { static struct book b1={"Let us C", "YPK", 101}; display(b1); } display(b) struct book b; { printf("%s\n%s\n%d", b.name, b.author,b.callno); } OUTPUT: Let us C YPK 101 structure*/ #include<stdio.h> main() { struct address

{ char phone[15]; char city[25]; int pin; }; struct emp { char name[25]; struct address a; }; static struct emp e={"jeru", "531046", "Nagpur", 10}; printf("name=%s phone=%s\n", e.name, e.a.phone); printf("city-%s pin=%d", e.a.city,e.a.pin); } OUTPUT: name: jeru phone= 531046 city nagpur pin=10 #include<stdio.h> #include<conio.h> #include<dos.h> main() { struct date d; struct time t; clrscr(); getdate(&d); gettime(&t); printf("\nYear : %d",d.da_year); printf("\tDay : %d",d.da_day); printf("\tMonth : %d",d.da_mon); printf("\n\nhoor : %d",t.ti_hour); printf("\tminutes : %d",t.ti_min); printf("\tseconds : %d",t.ti_sec); getch(); } lines in a given file */ #include<stdio.h> main() { FILE*fp; char ch; int nol=0, not=0, noc=0,nob=0; clrscr(); fp=fopen("charge.c","r"); while(1) { ch=getc(fp); if(ch== EOF) break; noc++; if(ch==' ') nob++; if(ch=='\n')

nol++; if(ch=='\t') not++; } fclose(fp); printf("Number of characters=%d\n",noc); printf("Number of blanks=%d\n",nob); printf("Number of tabs=%d\n",not); printf("Number of lince=%d",nol); } OUTPUT: Number of characters=500 printf("Number of blanks=20 printf("Number of tabs=5 printf("Number of lince=15 #include<stdio.h> main() { FILE *fp; char ch; clrscr(); fs=fopen("charge.c","r"); ft=fopen("charge1.c","w"); while(1) { ch=getc(fs); if(ch==EOF) break; else putc(ch,ft); } fclose(fs); fclose(ft); } line arguments */ #include<stdio.h> main(argc,argv) int argc; char *argv[]; { FILE *fs,*ft; char ch; clrscr(); fs=fopen(argv[1],"r"); ft=fopen(argv[2],"w"); while(1) { ch=getc(fs); if(ch==EOF) break; else

putc(ch,ft); } fclose(fs); fclose(ft); } #include<stdio.h> main() { FILE *fp; char another='y'; char name[40]; int age; float bs; fp=fopen("EMPLOYEE.DAT","w"); if(fp==NULL) { puts("cannot open file"); exit(); } while(another=='y') { printf("\nenter name,age and basic salary \n"); scanf("%s%d%f",&name,&age,&bs); fprintf(fp,"%s%d%f",name,age,bs); printf("\n Another employee(Y/N)"); fflush(stdin); another=getch(); } fclose(fp);} INPUT: enter name,age and basic salary vasu 45 1000 y krishna 25 2000 n #include<stdio.h> main() { FILE *fp; char another='y'; char name[40]; int age; float bs; fp=fopen("EMPLOYEE.DAT","r"); if(fp==NULL) { puts("cannot open file"); exit(); } while(fscanf(fp,"%s%d%f",&name,&age,&bs)!=EOF) printf("%s%d%f",name,age,bs); fclose(fp); }

OUTPUT: vasu 45 1000 krishna 25 2000 #include<stdio.h> main() { FILE *fp; char another='y'; struct emp { char name[40]; int age; float bs; }; struct emp e; fp=fopen("EMPLOYEE.DAT","w"); if(fp==NULL) { puts("cannot open file"); exit(); } while(another=='y') { printf("\nenter name,age and basic salary \n"); scanf("%s%d%f",e.name,&e.age,&e.bs); fprintf(fp,"%s%d%f\n",e.name,e.age,e.bs); printf("\n Add another record(Y/N)"); fflush(stdin); another=getch(); } fclose(fp); } INPUT: enter name,age and basic salary vasu 45 1000 y krishna 25 2000 n /*84. Write a C program to read the details of an employee using structures*/ #include<stdio.h> main() { FILE *fp; struct emp { char name[40]; int age; float bs; }; struct emp e; fp=fopen("EMPLOYEE.DAT","r"); if(fp==NULL) {

puts("cannot open file"); exit(); } while(fscanf(fp,"%s%d%f",&e.name,&e.age,&e.bs)!=EOF) printf("%s%d%f\n",e.name,e.age,e.bs); fclose(fp); } OUTPUT: vasu 45 1000 krishna 25 2000 of an employee in file*/ #include<stdio.h> main() { FILE *fp; char another='y'; struct emp { char name[40]; int age; float bs; }; struct emp e; fp=fopen("EMPLOYEE.DAT","wb"); if(fp==NULL) { puts("cannot open file"); exit(); } while(another=='y') { printf("\nenter name,age and basic salary \n"); scanf("%s%d%f",e.name,&e.age,&e.bs); fwrite(&e,sizeof(e),1,fp); printf("\n Add another record(Y/N)"); fflush(stdin); another=getch(); } fclose(fp); } INPUT: enter name,age and basic salary vasu 45 1000 y krishna 25 2000 n OUTPUT: of employee on VDU*/ #include<stdio.h> main() { FILE *fp; char another='y';

struct emp { char name[40]; int age; float bs; }; struct emp e; fp=fopen("EMPLOYEE.DAT","rb"); if(fp==NULL) { puts("cannot open file"); exit(); } while(fread(&e,sizeof(e),1,fp(==1) printf("%s%d%f",e.name,&e.age,&e.bs); fclose(fp); } OUTPUT: vasu 45 1000 krishna 25 2000 and listing the records in a C database*/ #include<stdio.h> main() { FILE *fp,*ft; char another,chioce; struct emp { char name[40]; int age; float bs; }; struct emp e; char empname[40],choice; fp=fopen("emp.dat","rb+"); if(fp==NULL); { fp=fopen("emp.dat","wb+"); if(fp==NULL) { puts("cannot open file"); exit(); } } while(1) { printf("\n1.ADD RECORDS"); printf("\n2.LIST RECORDS"); printf("\n3.MODIFY RECORDS"); printf("\n4.DELETE RECORDS"); printf("\n0.EXIT");

printf("\n\nYOUR CHOICE"); choice=getch(); switch(choice) { case'1': fseek(fp,0,SEEK_END); another='y'; while(another=='y') { printf("\nenter name,age and basic sal."); scanf("%s %d %f",e.name,&e.age,&e.bs); fwrite(&e,sizeof(e),1,fp); printf("\nadd another record(y/n)"); fflush(stdin); another=getch(); } break; case'2': clrscr(); rewind(fp); while(fread(&e,sizeof(e),1,fp)==1) printf("%s%d%f\n",e.name,e.age,e.bs); break; case'3': another='y'; while(another=='y') { printf("\nenter name of employee modify"); scanf("s",empname); rewind(fp); while(fread(&e,sizeof(e),1,fp)==1) { if(strcmp(e.name,empname)==0) { printf("\nenter new name,age&bs"); scanf("%s%d%f",e.name,&e.age,&e.bs); fseek(fp,sizeof(e),SEEK_CUR); fwrite(&e,sizeof(e),1,fp); break; } } printf("\nmodify another record(y/n)"); fflush(stdin); another=getch(); } break; case'4': another='y'; while(another=='y') { printf("\nenter name of employee to delete");

scanf("%s",empname); ft=fopen("TEMP.DAT","wb"); rewind(fp); while(fread(&e,sizeof(e),1,fp)==1) { if(strcmp(e.name,empname)!=0) fwrite(&e,sizeof(e),1,ft); } fclose(fp); fclose(ft); remove("EMP.DAT"); rename("TEMP.DAT","EMP.DAT"); fp=fopen("EMP.DAT","rb+"); printf("delete another record(y/n)"); fflush(stdin); another=getch(); } break; case'0': fclose(fp); exit(); } }!

Potrebbero piacerti anche