+ All Categories
Home > Documents > The c Program Book

The c Program Book

Date post: 07-Apr-2015
Category:
Upload: smanikandan1987
View: 153 times
Download: 2 times
Share this document with a friend
38
The C program book 1.PROGRAM FOR CELCIUS TO FAHRENHEIT: #include<stdio.h> #include<conio.h> void main() { float cel,faren; clrscr(); printf("Enter the Temperature in Celsius::"); scanf("%f",&cel); faren=(1.8*cel)+32; printf("The fahrenteiet value is: %f",faren); printf("Enter the Temperature in Fahrenheit:"); scanf("%f",&faren); cel=(faren-32)/1.8; printf("The Celcius Value is: %f",cel; getch(); } 2.Simple and compound interest: #include<stdio.h> #include<math.h> #include<conio.h> void main() { float p,n,r,i,si,ci; clrscr(); printf(" Enter the priciple:"); scanf("%f",&p); printf(" Enter the years:"); scanf("%f",&n); printf(" Enter the interest:"); scanf("%f",&r); si=p*n*r/100; printf("The simple Interest is:%f\n",si); i=r/100; ci=p*pow((1+i),n); printf("The compound Interest is:%f\n",ci); getch(); } 3.Eg for sizeof operator #include<stdio.h> #include<conio.h>
Transcript
Page 1: The c Program Book

The C program book

1.PROGRAM FOR CELCIUS TO FAHRENHEIT: #include<stdio.h> #include<conio.h> void main() { float cel,faren; clrscr(); printf("Enter the Temperature in Celsius::"); scanf("%f",&cel); faren=(1.8*cel)+32; printf("The fahrenteiet value is: %f",faren); printf("Enter the Temperature in Fahrenheit:"); scanf("%f",&faren); cel=(faren-32)/1.8; printf("The Celcius Value is: %f",cel; getch(); }

2.Simple and compound interest: #include<stdio.h> #include<math.h> #include<conio.h> void main() { float p,n,r,i,si,ci; clrscr(); printf(" Enter the priciple:"); scanf("%f",&p); printf(" Enter the years:"); scanf("%f",&n); printf(" Enter the interest:"); scanf("%f",&r); si=p*n*r/100; printf("The simple Interest is:%f\n",si); i=r/100; ci=p*pow((1+i),n); printf("The compound Interest is:%f\n",ci); getch(); }

3.Eg for sizeof operator #include<stdio.h> #include<conio.h> main() { int i=10; float f=25.005; char name[]="welcome"; clrscr(); printf("\n The size of integer is %d",sizeof(i)); printf("\n The size of float is %d",sizeof(f)); printf("\n The size of character is %d",sizeof(name)); getch();

Page 2: The c Program Book

}

4.ARMSTRONG NO: #include<stdio.h> #include<conio.h> void main() { int n,a,r, sum=0; clrscr(); printf(" enter the no:"); scanf(" %d",&n); a=n; while(n>0) {

r=n%10; sum=sum+r*r*r; n=n/10;

} if(a==sum)

printf("%d is an armsstrong no",sum); else

printf("its is not an armstrong no",); getch(); }

5.Decimal to binary : #include<stdio.h> #include<conio.h> main() { int bnum,digit,decimal=0,bin,base=0; clrscr(); printf("\nEnter the binary number:"); scanf("%d",&bnum); bin=bnum; while(bnum!=0) {

digit=bnum%10; decimal=decimal+(digit<<base); base=base+1; bnum=bnum/10;

} printf("\nThe binary equivalent of %d in decimal=%d",bin,decimal); getch(); }

6.Standard deviation: #include<stdio.h> #include<conio.h> #include<math.h> main() { int i,n; float val[30],tempvar,mean,var,sd; float sum=0; float sumvar=0; clrscr();

Page 3: The c Program Book

printf(" Enter the value of N:"); scanf("%d",&n); for(i=0;i<n;i++) {

scanf("%f",&val[i]); sum=sum+val[i];

} mean=sum/(float)n; for( i=1;i<=n;i++) {

tempvar=val[i]-mean; sumvar+=tempvar*tempvar;

} var=sumvar/(float)n; sd=sqrt(var); printf(" The of Items N is:%d\n",n); printf("Mean: %3.2f\n",mean); printf("Variance:%3.2f\n",var); printf("Standard Deviation :%3.2f\n",sd); getch(); }

7.STANDARD DEVIATION USING FUNCTIONS: #include<stdio.h> #include<math.h> void main() { int i,num; float dev,list[100]; float standev(); clrscr(); printf("\nEnter the size of the list"); scanf("%d",&num); printf("\nEnter the elements of the list"); for(i=0;i<num;i++)

scanf("%f",&list[i]); printf("\nEntered elements are"); for(i=0;i<num;i++)

printf("\n%f",list[i]); dev=standev(list,num); printf("\n Standard Deviatiion of the list is %10.5f\n",dev); getch(); } float standev(float lis[100],int no) { int i; float mean,dev,sum=0.0,var; float avg(); mean=avg(lis,no); printf("\n Mean of %3d elements is%10.2f\n",no,mean); for(i=0;i<no;i++)

sum=sum+(mean-lis[i])*(mean-lis[i]); dev=sqrt(sum/(float)no); return(dev); } float avg(float l[100],int n) {

Page 4: The c Program Book

int i; float sum=0.0; for(i=0;i<n;i++)

sum=sum+l[i]; return(sum/(float)n); }

8.Program for GCD #include<stdio.h> #include<conio.h> void main() { int i,j,k; clrscr(); printf(" enter two no"); scanf("%d%d", &i ,&j ); if(j>i) { k=i; i=j; j=k; } else { k=i%j; i=j; j=k; } printf(" the gcd is %d",j); }

9.Lagest of three numbers: #include<stdio.h> #include<conio.h> main() { int a,b,c; clrscr(); printf("Enter the three numbers"); scanf("%d %d %d",&a,&b,&c); if(a>b && a>c) printf(" %d is the greatest ",a); else if (b>a && b >c) printf(" %d is the greatest ",b); else printf("%d isthe greatest ",c); getch(); }

10.Program demonstrating if else if( gross salary) #include<stdio.h> #include<conio.h> void main() { float bs,hra,da,cc,gross; clrscr(); printf(" \n Enter basic salary:");

Page 5: The c Program Book

scanf("%f" ,&bs); if(bs<=3000) { hra=bs*10/100; da=bs*60/100; cc=500; printf("\nDESIGNATION: Lab assisant:"); } if(bs >5000 && bs <=8000) { hra=bs*15/100; da=bs*70/100; cc=700; printf("\n DESIGNATION:Lecturer:"); } else if(bs >8000 && bs<=12000) { hra=bs*25/100; da=bs*80/100; cc=1000; printf(" \nDESIGNATION: Asst Professor"); } else if(bs >12000) { hra=bs*25/100; da=bs*90/100; cc=1500; printf(" \nDESIGNATION: Professor"); } gross=bs+hra+cc+da; printf(" \n\nBASIC SALARY:%5.2f",bs); printf(" \n\nHRA: %5.2f",hra); printf(" \n\nDA: %5.2f", da); printf(" \n\nCONVEYANCE: %5.2f",cc); printf(" \n\nGROSS SALARY :%5.2f",gross); getch(); }

11.Sine series: #include<stdio.h> #include<math.h> #include<conio.h> main() { int no,i; float x,a,sum,b; clrscr(); printf("Enter the number : "); scanf("%f %d",&x,&no); b=x; x=x*3.141/180; a=x; sum=x; for(i=1;i<no+1;i++) { a=(a*pow((double)(-1),(double)(2*i-1))*x*x)/(2*i*(2*i+1)); sum=sum+a;

Page 6: The c Program Book

} printf("Sin(%f) value is %f",b,sum); getch(); }

12.Cosine series: #include<stdio.h> #include<math.h> #include<conio.h> main() { float x,a,sum=1,temp=1; int i,no=20,mul; clrscr(); printf("\nEnter the value of x:"); scanf("%f",&x); a=x; x=x*3.1412/180; temp=temp*pow((double)(-1),(double)(2*i-1))*x*x/(2*i*(2*i-1)); sum=sum+temp; printf("\nThe cosine value of %f is %f",a,sum); getch(); }

13.Eb bill calculations: #include<stdio.h> #include<conio.h> void main() { int PR,CR,unit; float cost; clrscr(); printf(" Enter the previous reading:\n"); scanf("%d",&PR); printf(" Enter the current reading:\n"); scanf("%d",&CR); unit =CR-PR; if(unit>250) { cost=unit*2.00; printf(" The total payment is Rs:%3.2f\n",cost); } else if((unit<100)&&(unit>250)) { cost=unit*1.50; printf(" The total payment is Rs: %3.2f\n",cost); } else{

cost=unit*1.00; printf(" The total payment is Rs:%3.2f\n",cost); } getch(); }

Page 7: The c Program Book

14.Menu driven calculator: #include<stdio.h> #include<conio.h> void main() { int n; float add,sub,mul,a,b,div; clrscr(); printf("Enter the values for a & b :"); scanf("%f%f",&a,&b); printf("\n Enter 1 for addition"); printf("\n Enter 2 for subtraction \n "); printf("\n Enter 3 for multiplication"); printf("\n Enter 4 for division\n"); scanf("%d",&n); switch(n) { case 1: printf("The values are %f %f",a,b); add=a+b; printf("\nThe resultant value is :%f",add); break; case 2: printf("The values are%f %f",a,b); sub=a-b; printf("\nThe resultant value is :%f",sub); break; case 3: printf("The values are %f %f",a,b); mul=a*b; printf("\nThe resultant value is :%f",mul); break; case 4: printf("The values are%f %f",a,b); div=a/b; printf("\nThe resultant value is :%5.2f",div); break; default :printf("ERROR :enter the values within the range"); }getch(); }

15.Switch case examples: #include<stdio.h> #include<conio.h> main() { int num,o,fact=1,i; clrscr(); while(1) {

printf("\nEnter the number: "); scanf("\n%d",&num); printf("\nchoose one of the options given below"); printf("\n1.FACTORIAL"); printf("\n2.PRIME NUMBER"); printf("\n3.ODD or EVEN"); printf("\n4.EXIT\n"); scanf("\n%d",&o); switch(o) {

case 1: for(i=1;i<=num;i++)

Page 8: The c Program Book

fact=fact*i; printf("The factorial of %d is %d",num,fact); break;

case 3: if(num%2==0) printf("The given number is Even number"); else printf("The given number is Odd number"); break;

case 2: i=2; while(i<=num-1) {

if(num%i==0) { printf("The given number is not a prime number"); break;

} i++; } if(i==num)

printf("The given number is a prime"); break;

default: exit(0); }

} }

16.To count no of vowels in string: #include<stdio.h> main() { int v=0,c=0,i=0; char str[25]; clrscr(); printf("Enter the string:"); scanf("%s",str); while(str[i]!='\0') {

switch(str[i]) {

case 'a': case 'A': case 'E': case 'I': case 'O': case 'U': case 'e': case 'i': case 'o': case 'u': v++; break;

default: c++;

}

Page 9: The c Program Book

i++; } printf("\nThe number of vowels is %d",v); printf("\nThe number of consonants is %d",c); getch(); }

17.Palindrome of a no: #include<stdio.h> #include<conio.h> main() { unsigned long int a,num,sum=0,rnum=0,rem; clrscr(); printf("\nEnter the number:"); scanf("%ld",&num); a=num; while(num!=0) {

rem=num%10; sum=sum+rem; rnum=rnum*10+rem; num=num/10;

} printf("\nThe sum of the digits of %ld is %ld\n",a,sum); printf("\nThe reverse number of the %ld is %ld",a,rnum); if(a==rnum)

printf("\nThe given number is a palindrome"); else

printf("\nThe given number is not a palindrome"); getch();

}

18.Palindrome of a string: #include<stdio.h> #include<stdlib.h> #include<conio.h> main() { int len=0,i,j; char name[25]; clrscr(); printf("Enter the string:"); scanf("%s",name); while(name[len]!='\0')

len++; printf("\n%d",len); for(i=0,j=len-1;i<len/2;i++,j--) {

if(name[i]!=name[j]) {

printf("\nThe given string is not a palindrom"); exit(0);

} else printf(" the given string is palindrome");

Page 10: The c Program Book

} }

19.Write a program to search an element from an array using binary search #include<stdio.h> #include<conio.h> void main() { int i,l,h,a[100],no,n,t,flag=1; printf("Enter the upper limit \n "); scanf("%d",&n); printf("Enter the elements in ascending order \n"); for(i=0;i<n;i++) scanf("%d",&a[i]); l=0; h=n-1; printf("Enter the element to be searched ………\n "); scanf("%d ",&t); while(l<=h) { m=(l+h)/2;

if(t<a[m]) h=m-1;

else if (t>a[m]) l=m+1; else { printf("\n Entered %d is in position %d",t,m+1); flag=2; break; } } if(flag==1) printf("\n Entered element is not in the array"); getch(); }

20.To check given no is prime or not: #include <stdio.h> #include<conio.h> main() { int num,i=2; clrscr(); printf("Enter the number:"); scanf("%d",&num); while(i<=num-1) {

if(num%i==0) {

printf("The given number is not a prime number"); break;

} i++; } if(i==num)

Page 11: The c Program Book

printf("The given number is a prime"); getch(); }

21.Quadratic equations: #include<stdio.h> #include<math.h> #include<conio.h> void main() { int a,b,c,d; float root1,root2; clrscr(); printf("Enter the values of a,b,c\n"); scanf("%d %d %d",&a,&b,&c); d=b*b-4*a*c; if(d>=0) { printf(" Real and postive root");

root1=(-b+sqrt(d))/(2*a); root2=(+b+sqrt(d))/(2*a); printf("The roots of the values a=%d,b=%d,c=%d are\nR1: %f

R2:%f",a,b,c,root1, root2); } else if(d==0) { printf(" The roots are equal"); root1=-b/2*a; since d==0; root1=root2; printf(" \nRoot1 =%d \n Root2 =%d"); } else

printf("The roots are imaginary"); getch();

}

22.Pascal triangle: #include<stdio.h> #include<conio.h> void main() { int i,j,k,m,n; clrscr(); printf("Enter the number of lines to be printed : "); scanf("%d",&n); for(i=0;i<n;i++) { for(k=n;k>i;k--) printf(" "); for(j=0;j<=i;j++) { if((j==0)||(i==0)) m=1; else m=m*(i-j+1)/j; printf("%4d",m); }

Page 12: The c Program Book

printf("\n\n"); } getch(); }

23.Ordinary factorial #include<stdio.h> #include<conio.h> main() {

long int fact=1,i,num; clrscr(); printf("Enter the number:"); scanf("%ld",&num); for(i=1;i<=num;i++) {

fact=fact*i; } printf("The factorial of %ld is %ld:",num,fact); getch();

}

24.Ordinary fibonacci: #include<stdio.h> #include<conio.h> main() { int num,fib=0,a=0,b=1,i; clrscr(); printf("Enter the number:"); scanf("%d",&num); printf("\n FIBBONACI SERIES is :\n"); if(num==0)

printf("0"); else {

for(i=0;i<num;i++) {

fib=fib+a; a=b; b=fib; printf("\n %d\t",fib);

} getch();

} }

25.Fibonnaci using recursion: #include<stdio.h> #include<conio.h> void fib(int n); void main() { int n; clrscr(); printf("Enter the limit :");

Page 13: The c Program Book

scanf("%d",&n); printf("\nThe Fibonacci numbers are"); fib(n); getch(); } void fib(int n) { int f; static int f1=-1,f2=1,i=1; if(n>0) { f=f1+f2; f1=f2; f2=f; printf("\n%d %d",i,f); i++; fib(n-1); } }

26.Factorial using recursion: #include<stdio.h> #include<conio.h> main() { int recur(int); int num,a; clrscr(); printf("Enter the number"); scanf("%d",&num); a=recur(num); printf("The factorial of the number %d is %d",num,a); getch(); } recur(int no) { int fact=1; if(no==1)

return(1); else fact=no*recur(no-1); }

27.Binary search using function: #include<stdio.h> #include<conio.h> int key; main() { int a[50], i, n, loc; int bin(int *,int,int); clrscr(); printf("Enter the size of the array : "); scanf("%d", &n); printf("\nEnter Array elements (Ascending Order) :\n\n"); for (i=0; i<n; i++)

scanf("%d", &a[i]); printf("\nEnter Element to be Searched : ");

Page 14: The c Program Book

scanf("%d", &key); loc=bin(a,0,n);

if(loc==0) printf("Unsuccessful search. %d not found. \n", key); else { printf("Successful search.\n"); printf("%d found at position %d. \n", key, loc); }getch(); } int bin(int b[],int low,int high) { static int mid; int i; if(low<=high) {

mid=(low + high)/2; if(key<b[mid]) { high = mid-1; bin(b,low,high); } else if(key>b[mid]) { low = mid+1; bin(b,low,high); } else if(key==b[mid]) return(mid+1);

} else

return(0); }

28.Program For Passing pointers to function: #include<stdio.h> #include<conio.h> main() { int a[10],i,no,sum=0; clrscr(); printf("Enter the size of array:"); scanf("%d",&no); printf("Enter the elements of the array:"); for(i=0;i<no;i++)

scanf("%d",&a[i]); for(i=0;i<no;i++)

printf("\n%d",a[i]); sum=add(&a[0],no); printf("\nThe sum of %d numbers is:%d",no,sum); getch(); } add(int *pt,int n) { int i,a=0; for(i=0;i<n;i++)

Page 15: The c Program Book

{ a=a+*pt; pt++;

} return(a); }

29.Passing Pointers in function using structure: #include<stdio.h> #include<conio.h> typedef struct { char name[50]; int reg; int avg; }record; void main() { void disp(record *pt); static record stud={"abc",1,78}; clrscr(); disp(&stud); } void disp(record *pt) { printf("\n%20s %5d %5d",pt->name,pt->reg,pt->avg); getch(); }

30.Program for Pointers and structure #include<stdio.h> #include<conio.h> void main() { struct stud { char name[50]; int reg; int avg; }; static struct stud std={"abc",1,78}; struct stud *ptr; clrscr(); ptr=&std; printf("\n%20s %5d %5d",std.name,std.reg,std.avg); printf("\n%20s %5d %5d",ptr->name,ptr->reg,ptr->avg); getch(); }

31.Program for calculating ratio : #include<stdio.h> float rat(int a,int b, int c); int dif(int a, int b); void main() { int x,y,z; Printf("Enter three values");

Page 16: The c Program Book

Scanf("%d%d%d",&x,&y,&z); Printf("%f",rat(x,y,y)); } Float rat(int a,int b, int c) { If(dif(b,c)) Return(a/(b-c)); Else Return(0); }

int dif(int m,int n) { If(m!=n) Return(1); Else Return(0); }

32.Sum of the array elements: #include<stdio.h> #include<conio.h> main() { int a[100],i,no,sum=0; float avg=0; clrscr(); printf("\nEnter the number of elements"); scanf("%d",&no); printf("Enter the numbers"); for(i=0;i<no;i++) {

scanf("%d",&a[i]); sum=sum+a[i];

} avg=(float)sum/no; printf("sum=%d\naverage=%f",sum,avg); getch(); }

33.Acending order: #include<stdio.h> #include<conio.h> main() { int num[100],no,i,j,a; clrscr(); printf("Enter Upper Limit:"); scanf("%d",&no); printf("Enter the numbers"); for(i=0;i<no;i++)

scanf("%d",&num[i]); for(i=0;i<no-1;i++) {

for(j=i+1;j<no;j++) {

if(num[i]<num[j])

Page 17: The c Program Book

{ a=num[i]; num[i]=num[j]; num[j]=a;

} }

} printf("\nThe ascending order of the given numbers"); for(i=0;i<no;i++) printf("\n\t%d",num[i]); printf("\n The descending number of the given numbers"); for(j=no-1;j>=0;j--) printf("\n\t%d",num[j]); getch(); }

34.Inserting elements in array: #include<stdio.h> #include<conio.h> main() { int a[100],no,i,pos,item; clrscr(); printf("\nEnter the size of the matrix:"); scanf("%d",&no); printf("\nEnter the elements of the matrix:"); for(i=0;i<no;i++)

scanf("%d",&a[i]); printf("\nEntered elements of the matrix is"); for(i=0;i<no;i++)

printf("\n%d",a[i]); printf("\nEnter the element and its position in the array"); scanf("%d\n%d",&item,&pos); no++; for(i=no;i>=pos;i--) a[i]=a[i-1]; a[pos]=item; printf("\n"); printf("\nArray after the insertion"); for(i=0;i<no;i++)

printf("\n%d",a[i]); getch();

}

35.Lagest and smallest element in array: #include<stdio.h> #include<conio.h> main() { int a[100],i,small,large,no; clrscr(); printf("Enter the nos:"); scanf("%d",&no); printf("Enter the elements of the array:"); for(i=0;i<no;i++)

scanf("%d",&a[i]); printf("\nThe elements of the array ");

Page 18: The c Program Book

for(i=0;i<no;i++) printf("\n%d",a[i]);

small=a[0]; large=a[0]; for(i=1;i<no;i++) {

if(a[i]>large) large=a[i];

else if(a[i]<small) small=a[i];

} printf("\nThe largest of the given array is %d",large); printf("\nThe smallest of the given array is %d",small); getch(); }

36.Matrix addition: #include<stdio.h> #include<conio.h> main() { int a[25][25],b[25][25],c[25][25],i,j,m,n; clrscr(); printf("Enter the rows and column of two matrixes:\n"); scanf("%d %d",&m,&n); printf("\nEnter the elements of A matrix:"); for(i=0;i<m;i++) {

for(j=0;j<n;j++) scanf("%d",&a[i][j]);

} printf("nEnter the elements of B matrix:"); for(i=0;i<m;i++) {

for(j=0;j<n;j++) scanf("%d",&b[i][j]);

} printf("\nThe elements of A matrix"); for(i=0;i<m;i++) {

printf("\n"); for(j=0;j<n;j++)

printf("\t%d",a[i][j]); } printf("\nThe elements of B matrix"); for(i=0;i<m;i++) {

printf("\n"); for(j=0;j<n;j++)

printf("\t%d",b[i][j]); } printf("\nThe addition of two matrixes"); for(i=0;i<m;i++) {

printf("\n"); for(j=0;j<n;j++) {

Page 19: The c Program Book

c[i][j]=a[i][j]+b[i][j]; printf("\t%d",c[i][j]);

}

} getch(); }

37.Matrix multiplication: #include<stdio.h> #include<conio.h> main() { int a[25][25],b[25][25],c[25][25],i,j,k,r,s; int m,n; clrscr(); printf("\nEnter the Rows and Columns of A matrix:"); scanf("%d %d",&m,&n); printf("\nEnter the Rows and Columns of B matrix:"); scanf("%d %d",&r,&s); if(m!=r)

printf("\nThe matrix cannot multiplied"); else {

printf("\nEnter the elements of A matrix"); for(i=0;i<m;i++) {

for(j=0;j<n;j++) scanf("\t%d",&a[i][j]);

} printf("\nEnter the elements of B matrix"); for(i=0;i<m;i++) {

for(j=0;j<n;j++) scanf("\t%d",&b[i][j]);

} printf("\nThe elements of A matrix"); for(i=0;i<m;i++) {

printf("\n"); for(j=0;j<n;j++) printf("\t%d",a[i][j]);

} printf("\n The elements of B matrix"); for(i=0;i<m;i++) {

printf("\n"); for(j=0;j<n;j++) printf("\t%d",b[i][j]);

} for(i=0;i<m;i++) {

printf("\n"); for(j=0;j<n;j++) {

c[i][j]=0; for(k=0;k<m;k++)

Page 20: The c Program Book

c[i][j]=c[i][j]+a[i][k]*b[k][j]; }

}

printf("The multiplication of two matrixes"); for(i=0;i<m;i++) {

printf("\n"); for(j=0;j<n;j++) printf("\t%d",c[i][j]);

} getch(); } }

38.Transpose of matrix: #include<stdio.h> main() { int i,j,a[25][25],row,col; printf("\nEnter the number of rows and column of matrix"); scanf("%d%d",&row,&col); printf("\nEnter the elements of the matrix"); for(i=0;i<row;i++) for(j=0;j<col;j++)

scanf("%d",&a[i][j]); printf("The given matrix"); for(i=0;i<row;i++) {

printf("\n"); for(j=0;j<col;j++) printf("\t%d",a[i][j]);

} printf("\nThe transpose of the given matrix"); for(i=0;i<row;i++) {

printf("\n"); for(j=0;j<col;j++) printf("\t%d",a[j][i]);

} }

39.Ascending and descending using function(passing arrays to function): #include<stdio.h> #include<conio.h> void ascorder(int n ,int x[]); void desorder(int n ,int x[]); main() { int i,n,x[100]; clrscr(); printf(" enter the limit:"); scanf("%d",n); ascorder(n,x); printf("ascending order:\n"); for(i=0;i<n;i++) printf("%d",x[i]); desorder(n,x);

Page 21: The c Program Book

printf("descending order:\n"); for(i=0;i<n;i++) printf("%d",x[i]); } void ascorder(int n, int x[]) { int i,j,temp; for(i=0;i<n-1;i++) for(j=1;j<n;j++) if(x[i]<x[j]) { temp=x[i] ; x[i]=x[j]; x[j]=temp; } return; }

void desorder(int n, int x[]) { int i,j,temp; for(i=0;i<n-1;i++) for(j=1;j<n;j++) if(x[i]>x[j]) { temp=x[i] ; x[i]=x[j]; x[j]=temp; } return; }

40.Sorting of names using passing arrays to function: #include<stdio.h> #include<string.h> void reorder(int n,char x[][12]); main() { int i,n=0; char x[][12]; printf(" enter the string"); printf(type END for finish"); do { printf(string %d",n+1); scanf("%s",x[n]); }while(strcmp(x[n++],"END"); n- - ; reorder(n,x); printf(" reordered list of strings"); for(i=0;i<n;i++) printf("string %d",i+1,x[i]]); void reorder(int n,charx[][12]) {

char temp[12]; int i, j;

Page 22: The c Program Book

for(i=1;i<n;i++) for(j=2;j<n;j++) if(strcmp(x[i],x[j])>0) { strcpy(temp,x[i]); strcpy(x[i],x[j]); strcpy(x[j ,temp); } return; }

41.Program for merging of arrays: #include<stdio.h> #include<conio.h> void main() { int a[50],b[50],c[100],i ,j,n,m,k, temp; int *ptra,*ptrb,*ptrc; ptra=&a[0]; ptrb=&b[0]; ptrc=&c[0]; printf(:Enter A and B Array's sizes \n"); scanf("%d\n%d",&n,&m); printf("Enter %d elements for A array\n",n); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("Enter %d elements for B array\n",n); for(i=0;i<m;i++) scanf("%d",&b[i]); for(i=0;i<n;i++) { *ptrc=*(ptra); ptra=ptra+1; ptrc=ptrc+1; } for(j=0;j<n+m;j++) { *ptrc=*(ptrb); ptrb=ptrb+1; ptrc=ptrc+1; } printf("The array A and B are merged and stored in array C. That stored elements are \n"); for(k=0;k<n+m;k++) printf("%d\t",c[k]); for(i=0;i<n+m-1;i++) for(j=i+1;j<n+m;j++) { if(c[i]>c[j]) {

temp=c[i]; c[i]=c[j]; c[j]=temp;

} } printf(" The merged elements in ascending order. \n"); for(i=0;i<n+m;i++)

Page 23: The c Program Book

printf("%d ",c[i]); }

42.POINTER TO AN ARRAY: # include<stdio.h> # include<stdlib.h> void main() {

int m,n,i,j; void readinput(int *a[10],int m,int n); void computesum(int *a[10],int *b[10],int *c[10],int m,int n); void writeoutput(int *c[10],int m,int n); int *a[10],*b[10],*c[10];

printf("\nHow many rows?"); scanf("%d",&m); printf("\nHow many columns?"); scanf("%d",&n); for(i=0;i<m;i++) {

a[i]= (int *) malloc ( n * sizeof(int)); b[i]= (int *) malloc ( n * sizeof(int)); c[i]= (int *) malloc ( n * sizeof(int));

}

printf("\n First matrix:"); readinput(a,m,n); printf("\n Second matrix:"); readinput(b,m,n); computesum(a,b,c,m,n); printf("\n Output matrix:"); writeoutput(c,m,n); }

void readinput(int *a[10],int m,int n) {

int i,j; for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",(*(a+i)+j));

} void readinput(int *a[10],int m,int n)

{ int i,j; for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",(*(a+i)+j));

} void computesum(int *a[10],int *b[10],int *c[10],int m,int n) {

int i,j; for(i=0;i<m;i++) for(j=0;j<n;j++) *(*(c+i)+j)=*(*(a+i)+j)+*(*(b+i)+j);

} void writeoutput(int *c[10],int m,int n) {

int i,j;

Page 24: The c Program Book

for(i=0;i<m;i++) { for(j=0;j<n;j++)

printf("%4d",*(*(a+i)+j)); printf("\n");

} }

43.Matrix addition using pointers: # include<stdio.h> # include<stdlib.h> void main() {

int m,n,i,j; void readinput(int *a[10],int m,int n); void computesum(int *a[10],int *b[10],int *c[10],int m,int n); void writeoutput(int *c[10],int m,int n); int *a[10],*b[10],*c[10]; clrscr();

printf("\nHow many rows?"); scanf("%d",&m); printf("\nHow many columns?"); scanf("%d",&n); for(i=0;i<m;i++) {

a[i]= (int *) malloc ( n * sizeof(int)); b[i]= (int *) malloc ( n * sizeof(int)); c[i]= (int *) malloc ( n * sizeof(int));

} printf("\n First matrix:"); readinput(a,m,n); printf("\n Second matrix:"); readinput(b,m,n); computesum(a,b,c,m,n); printf("\n Output matrix:"); writeoutput(c,m,n); getch(); } void readinput(int *a[10],int m,int n) {

int i,j; for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",(a[i]+j));

} void computesum(int *a[10],int *b[10],int *c[10],int m,int n) {

int i,j; for(i=0;i<m;i++) for(j=0;j<n;j++) *(c[i]+j)=*(a[i]+j)+ *(b[i]+j);

} void writeoutput(int *c[10],int m,int n) {

int i,j; for(i=0;i<m;i++)

Page 25: The c Program Book

{ printf("\n"); for(j=0;j<n;j++) printf("%4d",*(c[i]+j)); }

}

4 #include<stdio.h> #include<string.h> void reorder(int n,char *x[]); main() { int i,n=0; char *x[10]; printf(" enter the string"); printf(type END for finish"); do { x[n]=(char*)malloc(12*sizeof(char)); printf(string %d",n+1); scanf("%s",x[n]); }while(strcmp(x[n++],"END"); reorder(n--,x); printf(" reordered list of strings"); for(i=0;i<n;i++) printf("string %d",i+1,x[i]]); void reorder(int n,char *x[]) {

char *temp; int i, j; for(i=1;i<n;i++) for(j=2;j<n;j++) if(strcmp(x[i],x[j])>0) { temp=x[i]; x[i]=,x[j]); x[j ]=temp; } return; }

45.Call by reference: #include<stdio.h> #include<conio.h> main() { int x,y; clrscr(); printf("\nEnter the two numbers:"); scanf(" %d %d",&x,&y); printf("\nBefore swapping:x=%d\ty=%d",x,y); display(&x,&y); printf("\nAfter swapping :x=%d\ty=%d",x,y); getch(); } display(int *a,int *b)

Page 26: The c Program Book

{ int t; t=*a; *a=*b; *b=t; }

46.Student details processing using structure: #include<stdio.h> #include<conio.h> main() { struct student {

char name[25]; char regno[25]; int avg; char grade;

} stud[50],*pt; int i,no; clrscr(); printf("Enter the number of the students"); scanf("%d",&no); for(i=0;i<no;i++) {

printf("\n student[%d] information:\n",i+1); printf("Enter the name"); scanf("%s",stud[i].name); printf("\nEnter the roll no of the student"); scanf("%s",stud[i].regno); printf("\nEnter the average value of the student"); scanf("%d",&stud[i].avg); } pt=stud; for(pt=stud;pt<stud+no;pt++) {

if(pt->avg<30) pt->grade='D';

else if(pt->avg<50) pt->grade='C';

else if(pt->avg<70) pt->grade='B';

else pt->grade='A';

}

printf("\n"); printf("NAME \tREGISTER-NO \tAVERAGE \tGRADE\n"); for(pt=stud;pt<stud+no;pt++) {

printf("%-20s%-10s",pt->name,pt->regno); printf("%10d \t %c\n",pt->avg,pt->grade); } getch();

} )

Page 27: The c Program Book

47.Write a program using nested structure #include<stdio.h> void main() { struct date {

int day, year; char month[20];

}; struct employee {

int code; char name; float salary; struct data doj;

}; struct employee emp1; printf("\n\n"); printf("Enter Employee Code :"); scanf("%d",&emp1.code); printf("Enter Employee Name :"); scanf("%s",emp1.name); printf("Enter Employee Salary :"); scanf("%f",&emp1.salary); printf("Enter Employee Date of Joining :"); scanf("%d %s %d",&emp1.doj.day, emp1.doj.month, &emp1.doj.year); printf("The Employee Code is %d",emp1.code); printf("The Employee Name is %s",emp1.name); printf("The Employee Salary is %f",emp1.salary); printf("The Employee Date of Joining is %d %s %d",emp1.doj.day, emp1.doj.month, emp1.doj.year); }

48.Program for union: #include<stdio.h> #include<stdlib.h> #include<conio.h> main() { union id { char color; int size; }; struct dres { char manufacture[20]; float cost; union id descrip; }shirt, pant; clrscr(); printf("%d\n",sizeof(union id)); printf("%d\n",sizeof(struct dres)); shirt.descrip.color='W'; printf("\n%c\t%d",shirt.descrip.color,shirt.descrip.size); shirt.descrip.size=15;

Page 28: The c Program Book

printf("\n%c\t%d",shirt.descrip.color,shirt.descrip.size); getch(); }

49.Simple file program to print characters: #include<stdio.h> #include<conio.h> main() { FILE *fp; char c; clrscr(); fp=fopen("Write","w"); printf("Enter the text: "); printf(" \n Press Ctrl + Z to exit "); while((c=getchar())!=EOF)

putc(c,fp); fclose(fp); printf("The entered data is...."); fp=fopen("Write","r"); while((c=getc(fp))!=EOF)

printf("%c",c); fclose(fp); getch();

}

50.File program for inventory management: #include<stdio.h> #include<conio.h> main() { FILE *FP; int n,q,i; float p,v; char item[10]; FP=fopen("input","w"); printf("item number price quantity"); for(i=1;i<=3;i++) { fscanf(stdin,"%s%d%f%d",item,&n,&p,&q); fprintf(FP,"%s%d%f%d",item,n,p,q); } fclose(FP); FP=fopen("input","r"); printf("item number price quantity"); for(i=1;i<=3;i++) { fscanf(FP,"%s%d%f%d",item,&n,&p,&q); v=p*q; printf("%s%d%f%d%f",item,n,p,q,v); } fclose(FP); getch(); }

Page 29: The c Program Book

51.File program to process employ details: #include<stdio.h> #include<conio.h> void main() { FILE *fp,*ft; char c,a; struct emp {

char name[40]; int age; int bs;

}e; char empname[40]; clrscr(); fp=fopen("emp.txt","w+"); if(fp==NULL) { puts("cannot open the file"); exit(); } while(1) { clrscr(); printf("\n1.Add 2.list 3. modify 4.delete 0. exit\n"); printf("your choice:\n"); c=getch(); switch(c) { case '1': a='y';

while(a=='y') { printf("enter name , age,basic sal\n"); scanf("%s%d%d",e.name,&e.age,&e.bs); fprintf(fp,"%s%d%d",e.name,e.age,e.bs); printf("add another record(Y/N):"); a=getch(); } break;

case '2': rewind(fp); fscanf(fp,"%s%d%d",e.name,&e.age,&e.bs); printf("\n%s\t%d\t%d",e.name,e.age,e.bs); break;

case '3': a='Y'; while(a=='Y')

{ printf("enter name to modify"); scanf("%s",empname); rewind(fp); while(fread(&e,rsize,1,fp)==1) { if(strcmp(e.name,empname)==0) { printf("enter new name age & basic sal"); scanf("\n%s\t%d\t%d",e.name,&e.age,&e.bs); fseek(fp,-rsize,SEEK_CUR);

Page 30: The c Program Book

fwrite(&e,rsize,1,fp); break; } } printf("modify another record(Y/N):"); a=getch(); } break;

case '4': a='Y'; while(a=='Y')

{ printf("enter name to delete"); scanf("%s",empname); ft=fopen("temp.txt","w+"); rewind(fp); while(fread(&e,rsize,1,fp)==1) if(strcmp(e.name,empname)!=0) fwrite(&e,rsize,1,ft); fclose(fp); fclose(ft); remove("emp.txt"); rename("temp.txt","emp.txt"); fp=fopen("emp.txt","r+"); printf("delete another record(Y/N):"); a=getch(); } break;

case '0': fclose(fp); exit();

} getch(); } } 52.Write a program to illustrate a) ftell( )b) fseek( )c) rewind( ) #include<stdio.h> main() { long int x=0,m; FILE *fptr=fopen("content.txt","r"); if(fptr !=NULL) {

fseek(fptr,0L,0); while(x=getc(fptr) !=EOF) putchar(c); fseek(fptr,0L,2); x=ftell(fptr); rewind(fptr); x=ftell(fptr); printf("\n The size of the file is %d",x);

} else printf("\n The file does not exists"); fclose(fptr);

}

Page 31: The c Program Book

53. program to print arguments from command line #include <stdio.h> main(int argc, char *argv[]) int count; printf("argc = %d\n\n",argc); for (count=0;count<argc;counti) printf("argv[%d]: %s\n",count, argv[count]); }

54.Sorting names with string library functions: #include<stdio.h> #include<conio.h> #include<string.h> void main() { char name[30][30],temp[30]; int i,j,n; clrscr(); printf(" How many names to be entered:"); scanf("%d",&n); for(i=0;i<n;i++) scanf("%s",name[i]); for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(strcmp(name[i],name[j])>0) {

strcpy(temp,name[i]); strcpy(name[i],name[j]); strcpy(name[j],temp); }

} }

printf(" Names in alphabetical order:\n"); for(i=0;i<n;i++) printf("\n%s\n",name[i]); getch(); }


Recommended