c programs

121
/* PROGRAM TO EVALUATE THE FOLLOWING 2.5LOG10(X) +COS(32)+X2-Y2+√2XY */ #include<stdio.h> #include<math.h> /* main fuction begins */ main() { double z; int x,y; clrscr(); /* accepting inputs from user */ printf("Enter x,y"); scanf("%d%d",&x,&y); /* evaluating the expression */ z=(2.5)*log10(x)+cos(32)+abs(x*x)-abs(y*y)+sqrt(2)*x*y; /* printing the result */ printf("z=%f\n",z); getch(); } OUTPUT: Enter x,y 4 5 z=21.623645 Enter x,y 5 4 z=39.865920 M V B REDDY GITAM UNIVERSITY BANGALORE

Upload: malikireddy-bramhananda-reddy

Post on 31-Oct-2014

512 views

Category:

Documents


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: C PROGRAMS

/* PROGRAM TO EVALUATE THE FOLLOWING 2.5LOG10(X)+COS(32)+X2-Y2+√2XY */

#include<stdio.h>#include<math.h>

/* main fuction begins */main(){ double z; int x,y; clrscr();

/* accepting inputs from user */ printf("Enter x,y"); scanf("%d%d",&x,&y);

/* evaluating the expression */ z=(2.5)*log10(x)+cos(32)+abs(x*x)-abs(y*y)+sqrt(2)*x*y;

/* printing the result */ printf("z=%f\n",z); getch();}

OUTPUT:

Enter x,y45z=21.623645

Enter x,y54z=39.865920

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 2: C PROGRAMS

/* PROGRAM TO EVALUATE THE FOLLOWING EXPRESSION (A*X+B)/(A*X-B) */

/*Header files*/

#include<stdio.h>#include<math.h>

/*Main function begins*/main(){ float a,b,x,c; clrscr(); /*Accepting inputs from user*/ printf("Enter a,b,x:\n"); scanf("%f%f%f",&a,&b,&x); /*evaluating the expression*/ c=(a*x+b)/(a*x-b); /*printing the answer*/ printf("c=%f",c); getch(); }

OUTPUT:

Enter a,b,x:123c=5.000000

Enter a,b,x:222c=3.000000

Enter a,b,x:321c=5.000000

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 3: C PROGRAMS

/* PROGRAM TO EVALUATE THE FOLLOWING SERIES SUM OF 1+2+3+4+5..... */

/*Linking section*/#include<stdio.h>

/*Main function begins*/main(){ /*Declration section*/ int i,n,sum=0; clrscr(); /*Accepting inputs from user*/ printf("ENTER NUMBERS OF ELEMENTS"); scanf("%d",&n); /*evaluating the expression*/ for(i=1;i<=n;i++) sum=sum+i;

/*printing the answer*/ printf("\n"); printf("SUM OF %d ELEMENTS=%d",n,sum); getch(); }

OUTPUT:

ENTER NUMBERS OF ELEMENTS 10

SUM OF 10 ELEMENTS=55

ENTER NUMBERS OF ELEMENTS: 100

SUM OF 100 ELEMENTS=5050

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 4: C PROGRAMS

/* PROGRAM TO EVALUATE THE FOLLOWING ALGEBRAIC EXPRESSION (1/ALPHA*SQR(2)*PI*E-POW(X-M/SQRT(20*SIGMA,2))

*/

/*Linking section*/#include<stdio.h>#include<conio.h>#include<math.h>

/*Definition section*/#define pi 3.14#define e 2.718

/*Main function begins*/main(){

/*Declration section*/ int m,alpha,sigma,x; clrscr();

/*Accepting inputs from user*/ puts("Enter values of alpha,sigma,m,x"); scanf("%d%d%d%d",&alpha,&sigma,&m,&x);

/*printing the result*/ printf("%f",(1/alpha*sqrt(2)*pi*e-pow(x-m/sqrt(2)*sigma,2))); getch(); }

OUTPUT:

Enter values of alpha,sigma,m,x111111.983847

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 5: C PROGRAMS

/* PROGRAM TO READ X AND COMPUTE Y=1 FOR X>0 Y=0 FOR X=0 Y=1 FOR X<0 */

/* Linking section */#include<stdio.h>

/* main function begins*/main(){ /* Declaration section*/ int x,y; clrscr();

/*Accepting inputs from user*/ printf("enter x"); scanf("%d",&x); /*Logic evaluation*/ if(x>0) printf("Y=1"); else if(x<0)

printf("Y=-1"); else

if(x==0) printf("Y=0");

/*End of logical evaluation*/ getch();}

OUTPUT:

Case: 1 enter x 1 Y=1Case: 2 enter x -1 Y=-1Case: 3 enter x 0 Y=0

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 6: C PROGRAMS

/* PROGRAM TO COMPUTE THE NET AMOUNT TO BE PAID BY THE CUSTOMER USING SWITCH AND IF STATEMENT */

Purchase amount Discount(percentage)

Mail cloth Handloom Items

1-100 5.0

101-200 5.0 7.5

201-300 7.5 10.0

Above 300 10.0 15.0

#include<stdio.h>#include<math.h>

/*Main program*/main(){ int amt,cho,item=0; float finalamt,dis=0.0;

/*Accepts inputs from user*/ puts("\nEnter amount: "); scanf("%d",&amt); puts("Enter item number \n 1.Mill cloth and 2.Handloom cloth"); scanf("%d",&item);

/*Discount evaluation*/ switch(item) { case 1: if(amt>=1&&amt<=100)

dis=0.0; else if(amt>=101&&amt<=200)

dis=5.0; else

if(amt>=201&&amt<=300) dis=7.5;else dis=10.0; break;

case 2: if(amt>=1&&amt<=100) dis=5.0; else if(amt>=101&&amt<=200)

dis=7.5; else if(amt>=201&&amt<=300)

dis=10.0;

else dis=15.0; break;

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 7: C PROGRAMS

default: puts("invalid cloth selection"); exit(0); finalamt=amt-(dis/100.0)*amt;

/*printing the result*/printf("Discount is: %f\n",dis);printf("Total amount is:%f",finalamt);

} getch();}

OUTPUT:

Enter amount: 2000Enter item number 1.Mill cloth and 2.Handloom cloth2Discount is: 15.000000Total amount is:1700.000000

Enter amount:15000Enter item number 1.Mill cloth and 2.Handloom cloth1Discount is: 10.000000Total amount is:13500.000000

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 8: C PROGRAMS

/* PROGRAM TO ACCEPT TWO OPERANDS AND OPERATOR AND PERFORM THE OPERATION*/

#include<stdio.h>/*Main function begins*/

main(){int a,b,i,c;clrscr();

/*Accepting inputs from user*/printf("Enter a value:");scanf("%d",&a);printf("Enter b value:");scanf("%d",&b);printf("Enter your choice:");scanf("%d",&i);

/*Switch case begins*/switch(i){case 1:printf("YOUR CHOICE IS ADDITION");

c=a+b; printf("\n%d",c); break;case 2:printf("YOUR CHOICE IS SUBSTRACTION"); c=a-b; printf("\n%d",c); break;case 3:printf("YOUR CHOICE IS MULTIPLICATION"); c=a*b; printf("\n%d",c); break;case 4:printf("YOUR CHOICE IS DIVISION"); c=a/b; printf("\n%d",c); break;case 5:printf("YOUR CHOICE IS MODULO DIVISION"); c=a%b; printf("\n%d",c); break;default:puts("Enter correct choice"); }

/*Ending switch case*//*Printing the result*/

getch();}

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 9: C PROGRAMS

OUTPUT :

Enter a value: 5Enter b value:6Enter your choice:3YOUR CHOICE IS MULTIPLICATION30

Enter a value:3Enter b value:3Enter your choice:1YOUR CHOICE IS ADDITION6

Enter a value:6Enter b value:1Enter your choice:2YOUR CHOICE IS SUBSTRACTION5

Enter a value:9Enter b value:5Enter your choice:4YOUR CHOICE IS DIVISION1

Enter a value:19Enter b value:8Enter your choice:5YOUR CHOICE IS MODULO DIVISION3

/* PROGRAM TO REVERSE OF A GIVEN NUMBER */

main()

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 10: C PROGRAMS

{ int rev=0,rem,n,m; clrscr(); printf("enter the value of n:"); scanf("%d",&n); m=n; while(n>0) { rem=n%10; rev=rev*10+rem; n=n/10; } printf("reverse of %d is %d",m,rev); getch();}

OUTPUT :

enter the value of n:582reverse of 582 is 285

enter the value of n:583reverse of 583 is 385

enter the value of n:1352reverse of 1352 is 2531

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 11: C PROGRAMS

/* PROGRAM TO TEST WHETHER THE NUMBER IS POLINDROME OR NOT */

main(){ int rev=0,rem,m,n; clrscr(); printf("ENTER THE NUMBER : "); scanf("%d",&n); m=n; while(n>0) { rem=n%10; rev=rev*10+rem; n=n/10; } if(rev==m) printf("\n%d is POLINDROME",m); else printf("\n%d is not polindrome",m); getch();}

OUTPUT :

ENTER THE NUMBER : 1221

1221 is POLINDROME

ENTER THE NUMBER : 1121

1121 is not polindrome

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 12: C PROGRAMS

/* PROGRAM FOR FACTORIAL OF A GIVEN NUMBER */

main(){ int n; long int fact=1; clrscr(); printf("ENTER THE NUMBER :"); scanf("%d",&n); while(n>0) { fact=fact*n; n--; } printf("FACTORIAL OF NUMBER IS %d",fact); getch();}

OUTPUT :

ENTER THE NMUMBER :6FACTORIAL OF NUMBER IS 720

ENTER THE NUMBER :4FACTORIAL OF NUMBER IS 24

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 13: C PROGRAMS

/* PROGRAM TO SUM THE DIGITS OF A GIVEN NUMBER */

main(){ int n,p,m,sum=0; clrscr(); printf("Enter a number : "); scanf("%d",&n); m=n; while(n>0) { p=n%10; sum=sum+p; n=n/10; } printf("The Sum of the digits of %d is %d ",m,sum); getch();}

OUTPUT :

Enter a number : 145The Sum of the digits of 145 is 10

Enter a number : 143The Sum of the digits of 143 is 8

Enter a number : 1234The Sum of the digits of 1234 is 10

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 14: C PROGRAMS

/* PROGRAM TO PRINT THE FOLLOWING FORMAT

1

2 2

3 3 3

4 4 4 4

5 5 5 5 5 */

#include<stdio.h>/* Main program begins */

main(){ int i,j,n; clrscr(); /* Accepting input */ printf("Enter n : "); scanf("%d",&n);

/* Generating diagram */ for(i=1;i<=n;i++) { for(j=1;j<=i;j++) { printf("%d",i); } printf("\n"); }

/* Printing the diagram */ getch();}

OUTPUT :

Enter n : 5

12 23 3 34 4 4 45 5 5 5 5

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 15: C PROGRAMS

/* PROGRAM TO PRINT THE FOLLOWING FORMAT BY USING FOR LOOP

1

2 2

3 3 3

4 4 4 4

5 5 5 5 5.... */#include <stdio.h>

/* Main functions begins */main(){int i,j,k,n;clrscr();

/* Accepting inputs from user */printf("Enter n :");scanf("%d",&n);

/* Generating diagram using FOR loop */for(i=0;i<=n;i++) { for(k=1;k<=40-4*i/2;k++) printf(" "); for(j=i;j>=1;j--) printf("%4d",i); printf("\n "); } /* Printing the value */ getch();}

OUTPUT :

Enter n : 5

1 2 2 3 3 34 4 4 4

5 5 5 5 5

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 16: C PROGRAMS

/* PROGRAM TO PRINT FIBONACCI SERIES UP TO 'N' USING DO-WHILE LOOP */

#include<stdio.h>main(){ int n,prev=0,curr=1,next=0; clrscr(); printf("Enter n value: "); scanf("%d",&n); printf("\n The fibonacci series up to %d:\n",n); printf("\n %d%4d",prev,curr); do { next=prev+curr; printf("%4d",next); prev=curr; curr=next; }

while(curr<n-1);getch();

}

OUTPUT :

Enter n value: 9

The fibonacci series up to 9 :

0 1 1 2 3 5 8

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 17: C PROGRAMS

/* PROGRAM TO ARRAY IN REVERSE ORDER */

#include<stdio.h>#include<conio.h>main(){ int a[10],i,n; clrscr(); printf("Enter the size of the array :"); scanf("%d",&n); printf("Enter the array elements :"); for(i=0;i<n;i++)

scanf("%d",&a[i]); printf("Reverse order :"); for(i=n-1;i>=0;i--)

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

}

OUTPUT :

Enter the size of the array :6

Enter the array elements : 1 2 3 4 5 6

Reverse order : 6 5 4 3 2 1

Enter the size of the array :9

Enter the array elements : 1 4 7 8 5 2 3 6 9

Reverse order : 9 6 3 2 5 8 7 4 1

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 18: C PROGRAMS

/* PROGRAM PERFORM SUM OF ARRAY ELEMENTS */

#include<stdio.h>#include<conio.h>main(){ int a[15],i,n,sum=0; clrscr(); printf("\n ENTER THE SIZE OF THE ARRAY :"); scanf("%d",&n); printf("\n ENTER THE ELEMENTS OF THE ARRAY\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); /* print the elements of the array */ printf("\n THE ELEMENTS OF THE ARRAY ARE:\t"); for(i=0;i<n;i++) printf("%2d",a[i]); /* to calculate the sum */ for(i=0;i<n;i++) sum=sum+a[i]; printf("\n THE SUM OF THE ELEMENTS OF THE ARRAY IS %d",sum); getch();}

OUTPUT :

ENTER THE SIZE OF THE ARRAY : 5

ENTER THE ELEMENTS OF THE ARRAY1 2 2 1 9

THE ELEMENTS OF THE ARRAY ARE:1 2 2 1 9

THE SUM OF THE ELEMENTS OF THE ARRAY IS : 15

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 19: C PROGRAMS

/* PROGRAM TO SWAP TWO ARRAYS */

#include<stdio.h>#include<conio.h>main(){int a[10],b[10],x,i,temp;clrscr();printf("\n enter x value :");scanf("%3d",&x);printf("\n enter elements of A:\n");for(i=0;i<x;i++) scanf("%3d",&a[i]);printf("A elements are:\n");for(i=0;i<x;i++) printf("%3d",a[i]);printf("\n enter elements of B:\n");for(i=0;i<x;i++) scanf("%3d",&b[i]);printf("elements of B are:\n");for(i=0;i<x;i++) printf("%3d",b[i]);for(i=0;i<x;i++) { temp=a[i]; a[i]=b[i]; b[i]=temp; }printf("\n after swapping arays A&B\n");printf("\n A elements\n");for(i=0;i<x;i++) printf("\t%3d",a[i]);printf("\n elements of B are\n");for(i=0;i<x;i++) printf("\t%3d",b[i]);getch();}

OUTPUT :

enter x value :5enter elements of A: 5 1 7 9 3A elements are: 5 1 7 9 3enter elements of B: 1 4 0 6 4elements of B are: 1 4 0 6 4 after swapping arays A&B A elements 1 4 0 6 4 elements of B are 5 1 7 9 3

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 20: C PROGRAMS

/* PROGRAM TO PERFORM THE MATRIX TRANSPOSE */

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

int a[3][3],m,n,i,j,b[3][3];clrscr();printf("Enter the order of the Matrix");scanf("%d%d",&m,&n);printf("Enter the elements ");for(i=0;i<m;i++){ for(j=0;j<n;j++) scanf("%d",&a[i][j]);}for(i=0;i<m;i++) { for(j=0;j<n;j++) { b[j][i]=a[i][j]; } }printf("\n Matrix Transpose :\n");for(j=0;j<m;j++){ for(i=0;i<n;i++) { printf("%3d",b[j][i]); } printf("\n");}

getch(); }

OUTPUT :

Enter the order of the Matrix33Enter the elements 1 2 3 4 5 6 7 8 9

Matrix Transpose : 1 4 7 2 5 8 3 6 9

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 21: C PROGRAMS

/* PROGRAM TO IMPLEMENT MATRIX ADDITION */

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

int a[3][3],m,n,i,j,p,q,b[3][3],c[3][3];clrscr();printf("ENTER THE SIZE OF MATRIX 'A' \n");scanf("%d%d",&m,&n);printf("ENTER THE SIZE OF MATRIX 'B' \n");scanf("%d%d",&p,&q);

if(m==p&&n==q) {

printf("ENTER THE ELEMENTS OF THE MATRIX 'A' \n");for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]);printf("ENTER THE ELEMENTS OF THE MATRIX 'A' are");for(i=0;i<m;i++) { for(j=0;j<n;j++)

printf("%2d",a[i][j]); printf("\n");}printf("\nENTER THE ELEMENTS OF THE MATRIX 'B' \n");for(i=0;i<m;i++){ for(j=0;j<n;j++) scanf("%d",&b[i][j]);}

/* for printing matrix 'B' */printf("ENTER THE ELEMENTS OF THE MATRIX 'B' are");for(i=0;i<m;i++) { for(j=0;j<n;j++)

printf("%2d",b[i][j]); printf("\n");}

/*Matrix addition */for(i=0;i<m;i++){ for(j=0;j<n;j++)

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

/* To print matrix 'c' */printf("ENTER THE ELEMENTS OF THE MATRIX 'C' are");for(i=0;i<m;i++) { for(j=0;j<n;j++)

printf("%2d",c[i][j]); printf("\n");}}

else

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 22: C PROGRAMS

printf("MATRIX ADDITION IS NOT POSSIBLE \N"); getch();}

OUTPUT :

ENTER THE SIZE OF MATRIX 'A'2 2ENTER THE SIZE OF MATRIX 'B'2 2ENTER THE ELEMENTS OF THE MATRIX 'A'1 2 1 2ENTER THE ELEMENTS OF THE MATRIX 'A' are 1 2 1 2

ENTER THE ELEMENTS OF THE MATRIX 'B'2 3 2 3ENTER THE ELEMENTS OF THE MATRIX 'B' are 2 3 2 3ENTER THE ELEMENTS OF THE MATRIX 'C' are 3 5 3 5

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 23: C PROGRAMS

/* PROGRAM TO PERFORM MATRIX MULTIPLICATION*/

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

int a[5][5],m,n,i,j,p,k,q,b[5][5],c[5][5];clrscr();printf("ENTER THE ORDER OF MATRIX 'A' \n");scanf("%d%d",&m,&n);printf("ENTER THE ORDER OF MATRIX 'B' \n");scanf("%d%d",&p,&q);if(n==p) { printf("ENTER THE ELEMENTS OF THE MATRIX 'A' \n"); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); printf("\nENTER THE ELEMENTS OF THE MATRIX 'B' \n"); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&b[i][j]);

/* MATRIX MULTIPLICATON */ for(i=0;i<m;i++) { for(j=0;j<q;j++) { c[i][j]=0; for(k=0;k<n;k++) {

c[i][j]=c[i][j]+a[i][k]*b[k][j]; } } } printf(" MATRIX 'C' are\n"); for(i=0;i<m;i++) { for(j=0;j<q;j++) { printf("%4d\t",c[i][j]); } printf("\n"); } } else printf("\n Matrix multiplication is not possible"); getch();

}

OUTPUT :

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 24: C PROGRAMS

ENTER THE ORDER OF MATRIX 'A'3 3ENTER THE ORDER OF MATRIX 'B'3 3ENTER THE ELEMENTS OF THE MATRIX 'A'1 2 34 5 67 8 9

ENTER THE ELEMENTS OF THE MATRIX 'B'9 6 38 5 27 4 1 MATRIX 'C' are 46 28 10 118 73 28 190 118 46

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 25: C PROGRAMS

/* PROGRAM TO IMPLEMENT STRING SORTING */

#include<stdio.h>#include<string.h>main(){int i,j,n;char a[10][10],t=0;clrscr();printf("How many strings do you want to sort:");scanf("%d",&n);for(i=0;i<n;i++){ scanf("%s",a[i]);}for(i=0;i<n-1;i++){ for(j=i+1;j<n;j++) { if(strcmp(a[i],a[j])>0) {

strcpy(t,a[i]);strcpy(a[i],a[j]);strcpy(a[j],t);

} }}printf("strings after sorting");for(i=0;i<n;i++) printf("\n%s",a[i]);getch();}

OUTPUT :

How many strings do you want to sort:7SANTHOSHGANGADHARDINESHMANOJSATISHBHARGAVAMRNATH

strings after sortingAMARNATHBHARGAVDINESHGANGADHARMANOJSANTHOSHSATISH

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 26: C PROGRAMS

/* PROGRAM TO IMPLEMENT STRING POLINDROME*/

#include<stdio.h>#include<string.h>main(){char str1[20],str2[20];int i,j,n;clrscr();printf("Enter the string:\n\n");scanf("%s",str1);n=strlen(str1);for(j=0,i=n-1;i>=0;i--,j++) str2[j]=str1[i];str2[j]='\0';printf("\nReverse of string is %s\n",str2);if((strcmp(str1,str2))==0)

printf("\n Given string %s is a palindrome",str1);else

printf("\n Given string %s is not a palindrome\n",str1);getch();}

OUTPUT :

Enter the string:

SANTHOSH

Reverse of string is HSOHTNAS

Given string SANTHOSH is not a palindrome

Enter the string:

MADAM

Reverse of string is MADAM

Given string MADAM is a palindrome

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 27: C PROGRAMS

/* PROGRAM TO CHANGE FROM LOWER CASE TO UPPER CASE*/

#include<stdio.h>#include<ctype.h>#include<string.h>main(){char s[30];clrscr();printf("enter the string:\n");scanf("%s",s);convert(s);getch();} /*Function to convert all lower case letters to upper case*/convert(c)char c[];{int i,l;l=strlen(c); /* 1 holds the lentgh of string */for(i=0;i<l;i++){if(islower(c[i])) /* checking if the character is lower */c[i]=toupper(c[i]); /* converting the character to upper */}printf("\nThe string in upper case:");printf("\n%s",c);}

OUTPUT :

enter the string:sunny

The string in upper case:SUNNY

enter the string:santhosh

The string in upper case:SANTHOSH

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 28: C PROGRAMS

/* TO GET THE EXTRACTED PART OF A STRING */

#include <stdio.h>main(){char s1[30],s2[30];int i,j,m,n;clrscr();printf("Enter the string:\n");gets(s1);printf("\n Enter the value of m:\n"); /*m is starting position of extraction*/scanf("%d",&m);printf("Enter the value of n:\n");scanf("%d",&n);for(i=m,j=0;i<=(n+m-1);i++,j++)

s2[j]=s1[i];s2[j]='\0';printf("\n The extracted portion of the string1 is:%s",s2);getch();}

OUTPUT :

Enter the string:c and data structures

Enter the value of m:6Enter the value of n:15

The extracted portion of the string1 is: data structures

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 29: C PROGRAMS

/* PROGRAM TO IMPLEMENT STRING HANDLING FUNCTIONS*/

#include<stdio.h>#include<string.h>main(){ char str1[10],str2[10]; clrscr(); printf("ENTER STRING1 "); scanf("%s",str1); printf("\n1.LENGTH OF STRING1 :%d\n ",strlen(str1)); strcpy(str2,str1); printf("\n2.str1 and str2 , after copying str1 to str2"); printf("\nstr1=%s\nstr2=%s\n",str1,str2); if(strcmp(str1,str2)==0) { printf("\n3.STRINGS ARE EQUAL"); } else { if(strcmp(str1,str2)>0)

printf("str1 IS GREATER THAN str2"); else

printf("str2 IS GREATER THAN str1"); } strcat(str1,str2); printf("\n\n4.str1 AFTER ADDING str2 TO str1"); printf("\nstr1=%s",str1); getch();}

OUTPUT :

ENTER STRING1 : SANTHOSH

1.LENGTH OF STRING1 :8

2.str1 and str2 , after copying str1 to str2str1=SANTHOSHstr2=SANTHOSH

3.STRINGS ARE EQUAL

4.str1 AFTER ADDING str2 TO str1str1=SANTHOSHSANTHOSH

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 30: C PROGRAMS

/* PROGRAM TO FIND FACTORIAL OF A GIVEN NUMBER USING FUNCTIONS */

#include<stdio.h>#include<conio.h>/*Main function*/main(){ int y,n; clrscr(); printf("Enter any number:\n"); scanf("%d",&n); y=fact(n); printf("the value of %d is :%d",n,y); getch();}/*Factorial Function*/fact(int n1){ int i,f=1; for(i=1;i<=n1;i++) f=f*i; return(f);}

OUTPUT :

Enter any number:7the value of 7 is :5040

Enter any number:5the value of 5 is :120

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 31: C PROGRAMS

/* PROGRAM TO GENERATE A FIBONACII SERIES USING FUNCTION */

#include<stdio.h>#include<conio.h>main(){ int m; clrscr(); printf("Enter any number;\n"); scanf("%d",&m); fibb(m); getch();}fibb(int n){ int pre=1,curr=1,next,c=0; printf("%4d%4d",pre,curr);do{ next=pre+curr; printf("%4d",next); pre=curr; curr=next; c=c++;}while(c<n);}

OUTPUT :

Enter any number;5

1 1 2 3 5 8 13

Enter any number;9

1 1 2 3 5 8 13 21 34 55 89

/* PROGRAM TO FIND G.C.D OF TWO GIVEN NUMBERS USING FUNCTIONS */

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 32: C PROGRAMS

#include<stdio.h>#include<conio.h>main(){ int m,n,x; clrscr(); printf("Enter the values of m,n:\n"); scanf("%d%d",&m,&n); x=GCD(m,n); printf("\n the GCD of m=%d and n=%d is %d",m,n,x); getch();}GCD(int m,int n){ if(n==0) return(m); else if(n>m)

return GCD (n,m); else

return GCD (n,m%n);}

OUTPUT :

Enter the values of m,n:5430

the GCD of m=54 and n=30 is 6

Enter the values of m,n:6022

the GCD of m=60 and n=22 is 2

/* PROGRAM TO PRINT MTH FIBONACCI NUMBER */

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 33: C PROGRAMS

#include<stdio.h>main(){ int prev=0,curr=1,next=0,m,n=2; clrscr(); printf("Enter the mth value:"); scanf("%d",&m); printf("The Fibonacci Series is: \n"); printf("%d",prev); printf("%4d",curr); while(m>n) { next=curr+prev; printf("%4d",next); n++; prev=curr; curr=next; } getch();}

OUTPUT :

Enter the mth value:8The Fibonacci Series is:0 1 1 2 3 5 8 13

Enter the mth value:6The Fibonacci Series is:0 1 1 2 3 5

/* PROGRAM TO IMPLEMENT COMMAND LINE ARGUMENTS */

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 34: C PROGRAMS

#include<stdio.h>main(argc,argv)int argc;char *argv[]; { FILE *fp; int i; char word[15]; clrscr(); fp=fopen(argv[1],"w"); printf("\n No of arguments in command line=%d\n\n",argc); for(i=2;i<argc;i++)

fprintf(fp,"%s",argv[i]); fclose(fp); printf("Contents of %s file\n\n",argv[1]); fp=fopen(argv[1],"r"); for(i=2;i<argc;i++)

{ fscanf(fp,"%s",word); printf("%s\t",word); }

fclose(fp); printf("\n\n"); for(i=0;i<argc;i++) printf("%*.s\n",i*5,argv[i]); getch(); }

OUTPUT :

C:\TURBOC2>cmd.exe data computer science and engineering

No of arguments in command line=5

Contents of data

computer science and engineering

C:\TURBOC2\CMD.EXE Computer

science and

engineering

/* PROGRAM TO IMPLEMENT CALL BY VALUE */

#include <stdio.h>

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 35: C PROGRAMS

main(){ int a,b; clrscr(); printf("ENTER a,b VALUES\n"); scanf("%d%d",&a,&b); swap(a,b); printf("\nIn main\n"); getch();}

swap(int x,int y){ int t; t=x; x=y; y=t; printf("\n In sort function\n"); printf("\na=%d b=%d",x,y);}

OUTPUT :

ENTER a,b VALUES2 3 In sort function

a=3 b=2In mainA=2 ; B=3

/* PROGRAM TO IMPLEMENT CALL BY REFERENCE */

#include<stdio.h>main(){ int a,b;

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 36: C PROGRAMS

clrscr(); printf("ENTER a,b VALUES\n"); scanf("%d%d",&a,&b); swap(&a,&b); printf("a is %d\n",a); printf("b is %d\n",b); getch();}

swap(int *x,int *y){int t;t=*x;*x=*y;*y=t;}

OUTPUT :

ENTER a,b VALUES23A is 3B is 2

/* PROGRAM TO IMPLEMENT STRING CONCATENATION USING POINTERS */

#include<string.h>main(){ char a[10],b[10],c[20],*d,*e,*f; clrscr(); printf("ENTER TWO STRINGS\n");

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 37: C PROGRAMS

scanf("%s%s",a,b); d=&a[0]; e=&b[0]; f=&c[0]; while(*d!='\0') { *f=*d; f++; d++; } while(*e!='\0') { *f=*e; f++; e++; } *f='\0'; printf("%s",c); getch();}

OUTPUT :

ENTER TWO STRINGS :SANTHOSHMANOJ

SANTHOSHMANOJ

ENTER TWO STRINGS :BHARGAVGUPTA

BHARGAVGUPTA

/* PROGRAM TO IMPLEMENT POINTERS TO FUNTIONS */

main(){ int n,j; int fact(int); int (*ptr)(int); clrscr(); ptr=&fact; printf("\nENTER N VALUE\n"); scanf("%d",&n); j=(*ptr)(n);

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 38: C PROGRAMS

printf("\nFACTORIAL OF %d IS %d\n",n,j); getch();}

/*Factorial Function*/int fact(int x){ int i,fact1=1; for(i=1;i<=x;i++) { fact1=fact1*i; } return(fact1);}

OUTPUT :

ENTER n VALUE6FACTORIAL OF 6 IS 720

/* PROGRAM TO IMPLEMENT STRING COMPARISION USING POINTERS */

#include<string.h>main(){ int c;char a[10],b[10];clrscr();printf("Enter 2 strings");scanf("%s%s",a,b);c=xstrcmp(a,b);

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 39: C PROGRAMS

if(c==0)printf("strings are equal");elseprintf("strings are not equal");getch();}

xstrcmp(char *c,char *d){ while(*c==*d&&*c!='\0'&&*d!='\0'){c++;d++;}return(*c-*d);}

OUTPUT :

Enter 2 strings ManojManojStrings are equal

Enter 2 strings santhoshsantoshStrings are not equal

/* PROGRAM TO CALCULATE STRING LENGTH USING POINTERS*/

main(){ char str[10]; int y; clrscr(); printf("\nENTER NEW STRING\n"); scanf("%s",str); y=xstrlen(str); printf("\nLENGTH OF THE STRING IS:%d\n",y); getch();}int xstrlen(char *s)

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 40: C PROGRAMS

{ int len=0; while(*s!='\0') { s++; len++; } return(len);}

OUTPUT :

ENTER NEW STRINGGANGADHARLENGTH OF THE STRING IS 10

ENTER NEW STRINGDINESHLENGTH OF THE STRING IS 6

ENTER NEW STRINGSURYA TEJALENGTH OF THE STRING IS 5

/* PROGRAM TO INITIALIZE STRUCTURES */

#include<stdio.h>struct personal{ char name[20]; int day; char month[10]; int year; float salary;};main(){ struct personal person={"SANTHOSH",23,"september",2006,35000}; clrscr();

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 41: C PROGRAMS

printf("\nPerson details are:"); printf("\nname:%s\nday:%d\nmonth:%s\nyear:%d\nsalary:%f",person.name,person.day,person.month,person.year,person.salary); getch();}

OUTPUT :

Person details are:Name: SANTHOSHDay: 23Month: SeptemberYear: 2006Salary: 35000.000000

/* PROGRAM TO ACCEPT STRUCTURES */

struct personal{ char name[20]; int day; char month[10]; int year; float salary;};main(){ struct personal person; clrscr(); printf("\nenter values:"); printf("\nenter person name,day,month,year,salary:"); scanf("%s%d%s%d%f",person.name,&person.day,person.month,&person.year,&person.salary);

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 42: C PROGRAMS

printf("\nname:%s\nday:%d\nmonth:%s\nyear:%d\nsalary:%f",person.name,person.day,person.month,person.year,person.salary); getch();}

OUTPUT:

enter values:enter person name ,day ,month ,year ,salary :SANTHOSH05118825000

Name: SANTHOSHDay: 5Month: 11Year: 88Salary: 25000.000000

/* PROGRAM TO IMPLEMENT ARRAY OF STRUCTURES */

struct marks{ char name[20];int s1,s2,s3;int total;}student[3];

main(){ int i,n; clrscr();for(i=0;i<3;i++){ printf("\nEnter student[%d] details:",i+1); printf("\nEnter name:");

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 43: C PROGRAMS

scanf("%s",student[i].name); printf("Enter three subject ,marks:"); scanf("\n%d\n%d\n%d",&student[i].s1,&student[i].s2,&student[i].s3); student[i].total=student[i].s1+student[i].s2+student[i].s3; printf("%s\n%d\n%d\n%d\n",student[i].name,student[i].s1,student[i].s2,student[i].s3); printf("\nStudent[%d].total=%d\n",i+1,student[i].total);} getch();}

OUTPUT :

Enter student [1] details:

Enter name: AMARNATHEnter three subjects, marks: 88 83 96AMARNATH888396Student[1].total=267

Enter student[2] details:

Enter name: SANTHOSHEnter three subject ,marks:89 93 98SANTHOSH899398Student[2].total=280

/* PROGRAM TO PASS STRUCTURE MEMBERS AS ARGUMENTS TO FUNCTION */

struct account{ char name[10]; int acctno; float bal;}cus;

main(){ float adjust(); clrscr(); printf("enter the customer details:\n"); printf("enter customer name, account number,balance:\n"); scanf("%s%d%f",cus.name,&cus.acctno,&cus.bal); cus.bal=adjust(); printf("name:%s\naccount number:%d\ncustomerbalance:%f",cus.name,cus.acctno,cus.bal); getch();

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 44: C PROGRAMS

}

float adjust(){ float newbal; printf("\nenter newbal:"); scanf("%f",&newbal); return(newbal);}

OUTPUT :

enter the customer details:enter customer name, account number,balance:SATISH5825000

enter newbal:4012name: SATISHaccount number:582customerbalance:4012.000000

/* PROGRAM TO PASS COMPLETE STRUCTURE AS AN ARGUMENT TO A FUNCTION */

struct stores{

char name[20]; float price; int quantity;}item;

main(){ struct stores update(); float mul(),p_increment,value; int q_increment; struct stores item1; clrscr(); strcpy(item.name,"XYZ"); item.price=25.75; item.quantity=12; printf("Initial item values:\n\n");

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 45: C PROGRAMS

printf("Item name:%s\nItem price:%f\nItem quantity:%d\n",item.name,item.price,item.quantity); printf("\n\nInput increment values:\n"); printf("Price increment and quantity increment\n"); scanf("%f%d",&p_increment,&q_increment); item=update(item,p_increment,q_increment); printf("Update values of item\n\n"); printf("Name :%s\n",item.name); printf("Price :%f\n",item.price); printf("Quantity :%d\n",item.quantity); value = mul(item); printf("\nValue of the item=%f\n",value); getch();}struct stores update(product,p,q)struct stores product;float p;int q; { product.price+=p; product.quantity+=q; return(product); } float mul(stock)struct stores stock; { return(stock.price*stock.quantity);}

OUTPUT :

Initial item values:

Item name:XYZItem price:25.750000Item quantity:12

Input increment values:

Price increment and quantity increment1012

Update values of item

Name :XYZPrice :35.750000Quantity :24

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 46: C PROGRAMS

Value of the item=858.000000

/* PROGRAM TO IMPLEMENT SELF-REFERENTIAL STRUCTURES*/

#include<stdio.h>struct student{ char name[20]; int tot; struct student *p;};struct student s1,s2;main(){ clrscr(); printf("\nEnter First student name &total:\n"); scanf("%s%d",&s1.name,&s1.tot); printf("\nEnter second student name &total:\n"); scanf("%s%d",&s2.name,&s2.tot); s1.p=&s2; printf("\nFirst student details are:"); printf("%s %d",s1.name,s1.tot); printf("\nSecond student details are:"); printf("%s %d",s1.p->name,s1.p->tot); getch();}

OUTPUT:

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 47: C PROGRAMS

Enter First student name &total:BHARGAV599

Enter second student name &total:SANTHOSH600

First student details are: BHARGAV 599Second student details are: SANTHOSH 600

/* PROGRAM TO PERFORM READING AND WRITING TEXT TO A FILE */

#include<stdio.h> main(){ char c; FILE *fp; clrscr(); fp=fopen("INPUT","w"); while((c=getchar())!=EOF) putc(c,fp); fclose(fp); fp=fopen("INPUT","r"); while((c=getc(fp))!=EOF) putchar(c); fclose(fp); getch();}

OUTPUT :

I BELONGS TO C.S.E^ZI BELONGS TO C.S.E

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 48: C PROGRAMS

/* PROGRAM TO IMPLEMENT FILE APPENDING */

#include<stdio.h>main( ){ FILE *fp;

char c;clrscr();fp=fopen("BOOK1","w");while((c=getchar())!=EOF)putc(c,fp);fclose(fp);printf("\n\nText in the file book\n");fp=fopen("BOOK1","r");while((c=getc(fp))!=EOF)putchar(c);fclose(fp);fp=fopen("BOOK1","a");fprintf(fp,"HELLO");fclose(fp);printf("\nAfter appending\n");fp=fopen("BOOK1","r");while((c=getc(fp))!=EOF)putchar(c);fclose(fp);getch();

}

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 49: C PROGRAMS

OUTPUT :

C AND DATA STRUCTURES^Z

Text in the file bookC AND DATA STRUCTURES

After appendingC AND DATA STRUCTURESHELLO

/* PROGRAM TO WRITE NUMBERS TO A FILE & SEPERATE EVEN AND ODD FROM THE FILE & WRITE TO EVEN &ODD FILES */

#include<stdio.h>main(){ FILE *f1,*f2,*f3; int number,i,n,a[20]; clrscr(); f1=fopen("number","w"); f2=fopen("even","w"); f3=fopen("odd","w"); printf("\nenter n\n"); scanf("%d",&n); printf("\nenter %d numbers",n); for(i=0;i<n;i++) { scanf("%d",&a[i]); putw(a[i],f1); if(a[i]%2==0) putw(a[i],f3); else putw(a[i],f2); } fclose(f1); fclose(f2); fclose(f3); f1=fopen("number","r"); printf("\nfile contents :\n"); for(i=0;i<n;i++) { number=getw(f1); printf("\n%d",number); }

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 50: C PROGRAMS

fclose(f1); f2=fopen("odd","r"); printf("\n EVEN file contents:\n"); while((number=getw(f2))!=EOF) printf("\n%2d",number); fclose(f2); f3=fopen("even","r"); printf("\nODD file contents:\n"); while((number=getw(f3))!=EOF) printf("%2d",number); fclose(f3);}

OUTPUT :

enter n:4enter 4 numbers4 5 6 7file contents :4 5 6 7EVEN file contents:4 6 ODD file contents:5 7

/* PROGRAM TO PERFORM READING AND WRITING INTEGERS TO A FILE */

#include<stdio.h>main(){int a[10],i,n;FILE *fp;clrscr();printf("enter n value\t");scanf("%d",&n);fp=fopen("input","w");for(i=0;i<n;i++){printf("enter %d number\t",i);scanf("%d",&a[i]);putw(a[i],fp);}fclose(fp);fp=fopen("input","r");printf("\nfile contents\n");for(i=0;i<n;i++){fscanf(fp,"%d",&a[i]);printf("%d\n",a[i]);}fclose(fp);getch();}

OUTPUT:

enter n value 5enter 0 number 0

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 51: C PROGRAMS

enter 1 number 2enter 2 number 1enter 3 number 3enter 4 number 4

file contents02134

/* PROGRAM TO IMPLEMENT STACK OPERATION */

#include<stdio.h>int stack[10];int top=-1,n,i;main(){ int ch,item,ele; void push(); int pop(); void display(); clrscr(); printf("enter the size of stack:"); scanf("%d",&n); printf("\n1.Insert an element to the stack\n"); printf("\n2.delete an element from the stack\n"); printf("\n3.Display the contents\n"); printf("\n4.Quit the program\n"); while(1) {

printf("\n Enter the choice(1,2,3,4) : ");scanf("%d",&ch);

switch(ch) { case 1:if(top==n-1)

{ printf("\n Stack is full\n"); } else { printf("\n Enter the item to be inserted\n"); scanf("%d",&item); push(item); } break;

case 2:if(top==-1) { printf("\n Stack under flow");

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 52: C PROGRAMS

} else { ele=pop(); printf("Item deleted is:%d\n",ele); } break;

case 3:if(top==-1) { printf("stack is empty"); } else display(); break;

default:exit(); }}getche();}void push(int ch){

top++;stack[top]=ch;

}int pop(){

int item;item=stack[top];top--;return(item);

}void display(){

for(i=0;i<=top;i++)printf("%3d",stack[i]);

}

OUTPUT:

enter the size of stack:31.Insert an element to the stack2.delete an element from the stack3.Display the contents4.Quit the program

Enter the choice(1,2,3,4) : 1 Enter the item to be inserted10 Enter the choice(1,2,3,4) : 1 Enter the item to be inserted20 Enter the choice(1,2,3,4) : 1 Enter the item to be inserted30 Enter the choice(1,2,3,4) : 1 Stack is full

Enter the choice(1,2,3,4) : 3 10 20 30

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 53: C PROGRAMS

Enter the choice(1,2,3,4) : 2Item deleted is:30 Enter the choice(1,2,3,4) : 2Item deleted is:20Enter the choice(1,2,3,4) : 2Item deleted is:10 Enter the choice(1,2,3,4) : 2 Stack under flow

Enter the choice(1,2,3,4) : 3stack is empty

Enter the choice(1,2,3,4) : 4

/* PROGRAM TO CONVERT INFIX EXPRESSION TO PREFIX EXPRESSION */

#include<stdio.h>#include<conio.h>#include<ctype.h>#define max 20char stack[max];int top=-1;void push(char ch){ top++; stack[top]=ch;}int pop(){ char ch; ch=stack[top]; top--; return(ch);}int prec(char ch){ switch(ch) { case '(': case ')':return(0);

break; case '+': case '-':return(1);

break; case '*': case '/':return(2);

break; case '$':return(3);

break; }}void main(){ int i,j=0; char infix[25],post[25],pre[25];

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 54: C PROGRAMS

clrscr(); printf("ENTER THE INFIX EXPRESSION AND INSERT '(' AT THE BEGINNING\n"); gets(infix); push(')'); strrev(infix); for(i=0;infix[i]!='\0';i++) { if(isalpha(infix[i])) { post[j]=infix[i]; j++; } else { switch(infix[i])

{ case ')':push(')');

break; case '(':while(stack[top]!=')')

{ post[j]=pop(); j++; } pop(); break;

default:while(prec(infix[i])<=prec(stack[top])) { post[j]=pop(); j++; } push(infix[i]); break;

} }}post[j]='\0';strrev(post);strcpy(pre,post);printf("\n THE PREFIX STRING IS :");puts(pre);getch();}

OUTPUT:

ENTER THE INFIX EXPRESSION AND INSERT '(' AT THE BEGINNINGA-B/(C*D$E)

THE PREFIX STRING IS :A/B*C$DE

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 55: C PROGRAMS

/* PROGRAM TO CONVERT GIVEN INFIX EXPRESSION TO POSTFIX EXPRESSION */

#include<stdio.h>#include<ctype.h>char stack[100];int top=-1;main(){ char infix[100],postfix[100],ch; int i,j; void push(char ); char pop(); int prec(char c); int isoperator(char c); clrscr(); printf("ENTER INFIX EXZPRESSION ONLY:\n"); gets(infix); i=0,j=0; while((ch=infix[i++])!='\0') { if(ch=='(') push(ch); else if(isalpha(ch))

postfix[j++]=ch; else if(ch==')') {

while(stack[top]!='(') postfix[j++]=pop(); pop();

} else if(isoperator(ch))

{ while(isoperator(stack[top]&&prec(ch)<=prec(stack[top])))

postfix[j++]=pop(); push(ch); }

} while(top!=-1) postfix[j++]=pop(); postfix[j]='\0'; printf("POSTFIX EXPRESSION OF GIVEN INFIX EXPRESSION IS %s",postfix);

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 56: C PROGRAMS

getch();}void push(char c){ stack[++top]=c;}char pop(){ return(stack[top--]);}int prec(char c){ if(c=='+'||c=='-') return(1); if(c=='*'||c=='/') return(2); if(c=='$') return(3);}int isoperator(char c){ switch(c) { case '+': case '-': case '*': case '/': case '$':

return(1); default:

return(0); }}

OUTPUT:

ENTER INFIX EXZPRESSION ONLY:(a*b)+(d*e)POSTFIX EXPRESSION OF GIVEN INFIX EXPRESSION IS ab*de*+

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 57: C PROGRAMS

/* PROGRAM TO EVALUATE POSTFIX EXPRESSION */

#include<stdio.h>#include<ctype.h>#include<conio.h>#include<math.h>#define max 20float stack[max];int top=-1;void push(float x){top++;stack[top]=x;}float pop(){float x;x=stack[top];top--;return(x);}void main(){int i,j,k;float x,n,y;char post[25];clrscr();printf("enter the postfix expression\n");gets(post);for(i=0;post[i]!='\0';i++){if(isalpha(post[i])){printf("enter the value of %c",post[i]);scanf("%f",&n);push(n);}else{/*if the scanned character is an operator then pop the top two elements fromthe stack and then push the result to stack*/switch(post[i]){case'+':x=pop();

y=pop();x=x+y;push(x);break;

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 58: C PROGRAMS

case'-':x=pop();y=pop();x=y-x;push(x);break;

case'*':x=pop();y=pop();x=x*y;push(x);break;

case'/':x=pop();y=pop();x=y/x;push(x);break;

case'$':k=pop();j=pop();x=pow(j,k);push(x);break;}}

}printf("\n the result of evaluation is:%f",pop());getche();}

OUTPUT:

enter the postfix expressionABC*D/+enter the value of A 2enter the value of B 3enter the value of C 4enter the value of D 2

the result of evaluation is:8.000000

enter the postfix expressionABC*D/+enter the value of A 0enter the value of B 5enter the value of C 8enter the value of D 2

the result of evaluation is:20.000000

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 59: C PROGRAMS

/* PROGRAM TO EVALUATE PREFIX EXPRESSION */

#include<stdio.h>#include<ctype.h>#include<conio.h>#include<math.h>#define max 20float stack[max];int top=-1;void push(float x){ top++; stack[top]=x; }float pop(){ float x; x=stack[top]; top--; return(x); }void main(){ int i,j,k,l; float x,n,y; char pre[25]; clrscr(); printf(" enter the prefix expression\n"); gets(pre); l=strlen(pre); for(i=l-1;i>=0;i--) { if(isalpha(pre[i])) { printf("enter the value of%c",pre[i]); scanf("%f",&n); push(n); } else { /*if the scanned character is an operator then pop the two elements

from the stack and perform the desired computation and then push the result to stack*/switch(pre[i]){ case '+':x=pop();

y=pop(); x=x+y; push(x); break;

case '-':x=pop();

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 60: C PROGRAMS

y=pop(); x=x-y; push(x); break;

case '*':x=pop(); y=pop(); x=x*y; push(x); break;

case '/':x=pop(); y=pop(); x=x/y; push(x); break;

case '$':k=pop(); j=pop(); x=pow(j,k); push(x); break;

} } } printf("\n the result of evaluation id %f",pop()); getch(); }

OUTPUT:

enter the prefix expression+A/*BCDenter the value ofD 2enter the value ofC 4enter the value ofB 3enter the value ofA 2

the result of evaluation id 8.000000

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 61: C PROGRAMS

/* PROGRAM TO IMPLEMENT ALL THE QUEUE OPERATIONS USING ARRAYS */

#include<stdio.h>#define max 5int q[max],front=0,rear=-1,item;void insert();void delete();void display();main(){ int ch; clrscr(); while(1) { printf("1.INSERT 2.DELETE 3.DISPLAY 4.EXIT\n"); printf("enter your choice : "); scanf("%d",&ch); switch(ch) {

case 1:insert(); break;

case 2:delete(); break;

case 3:display(); break;

case 4:exit(0); default:printf("invalid choice\n");

} }}void insert(){ if(rear==max-1)

printf("Queue is full\n"); else {

printf("enter item\n");scanf("%d",&item);rear++;q[rear]=item;

}}void delete(){ if(front>rear)

printf("queue is empty\n"); else {

item=q[front];front++;printf("deleted item is:%d\n",item);

}}void display(){int i;

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 62: C PROGRAMS

if(front>rear)printf("Queue is empty\n");

else {

printf("the queue is:");for(i=front;i<=rear;i++)printf("%d\t",q[i]);printf("\n");

}}

OUTPUT :

1.INSERT 2.DELETE 3.DISPLAY 4.EXITenter your choice : 1enter item201.INSERT 2.DELETE 3.DISPLAY 4.EXITenter your choice : 1enter item401.INSERT 2.DELETE 3.DISPLAY 4.EXITenter your choice : 1enter item601.INSERT 2.DELETE 3.DISPLAY 4.EXITenter your choice : 1enter item551.INSERT 2.DELETE 3.DISPLAY 4.EXITenter your choice : 1enter item461.INSERT 2.DELETE 3.DISPLAY 4.EXITenter your choice : 3the queue is:20 40 60 55 461.INSERT 2.DELETE 3.DISPLAY 4.EXITenter your choice : 1Queue is full1.INSERT 2.DELETE 3.DISPLAY 4.EXITenter your choice : 2deleted item is:201.INSERT 2.DELETE 3.DISPLAY 4.EXITenter your choice : 2deleted item is:401.INSERT 2.DELETE 3.DISPLAY 4.EXITenter your choice : 2deleted item is:601.INSERT 2.DELETE 3.DISPLAY 4.EXITenter your choice : 2deleted item is:551.INSERT 2.DELETE 3.DISPLAY 4.EXITenter your choice : 2deleted item is:461.INSERT 2.DELETE 3.DISPLAY 4.EXITenter your choice : 2queue is empty1.INSERT 2.DELETE 3.DISPLAY 4.EXITenter your choice 4

/* PROGRAM TO IMPLEMENT ALL THE CQUEUE OPERATIONS USING ARRAYS */

#include<stdio.h>

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 63: C PROGRAMS

#define MAX 5int q[MAX],front=0,rear=-1,item;void insert();void delete();void display();main(){ int ch=1; clrscr(); while(ch!=4) { printf("\n 1.INSERT,2.DELETE,3.DISPLAY,4.EXIT\n"); fflush(stdin); printf(" enter your choice:"); scanf("%d",&ch); switch(ch) { case 1:insert();

break; case 2:delete();

break; case 3:display();

break; case 4:exit(0); default:printf(" INVALID CHOICE"); } } } void insert() { if((rear==MAX-1)&&(front==0)||((front==rear+1)&&(rear!=0&&front!=0))) printf("c queue is full\n"); else { printf(" enter item\n"); scanf("%d",&item); rear=(rear+1)%MAX; q[rear]=item; } } void delete() { if(front==0&&rear==-1)

printf(" queue is empty\n"); else { item=q[front];

if(front!=rear) front=(front+1)%MAX;else{ front=0; rear=-1; }printf(" deleted item is:%d\n",item);}}

void display(){

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 64: C PROGRAMS

int i; if((rear==-1)&&(front==0))

printf(" cqueue is empty"); else

{ if(front>rear) { for(i=0;i<=rear;i++)

printf("%4d",q[i]); for(i=front;i<MAX;i++)

printf("%4d",q[i]); } else { for(i=front;i<=rear;i++)

printf("%4d",q[i]); printf("\n"); } } }

OUTPUT :

1.INSERT,2.DELETE,3.DISPLAY,4.EXIT enter your choice:1 enter item11

1.INSERT,2.DELETE,3.DISPLAY,4.EXIT enter your choice:1 enter item12

1.INSERT,2.DELETE,3.DISPLAY,4.EXIT enter your choice:1 enter item13

1.INSERT,2.DELETE,3.DISPLAY,4.EXIT enter your choice:1 enter item14

1.INSERT,2.DELETE,3.DISPLAY,4.EXIT enter your choice:1 enter item15

1.INSERT,2.DELETE,3.DISPLAY,4.EXIT enter your choice:3 11 12 13 14 15

1.INSERT,2.DELETE,3.DISPLAY,4.EXIT enter your choice:1c queue is full

1.INSERT,2.DELETE,3.DISPLAY,4.EXIT enter your choice:2 deleted item is:11

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 65: C PROGRAMS

1.INSERT,2.DELETE,3.DISPLAY,4.EXIT enter your choice:2 deleted item is:12

1.INSERT,2.DELETE,3.DISPLAY,4.EXIT enter your choice:3 13 14 15

1.INSERT,2.DELETE,3.DISPLAY,4.EXITenter your choice:1 enter item10

1.INSERT,2.DELETE,3.DISPLAY,4.EXIT enter your choice:3 10 13 14 15 1.INSERT,2.DELETE,3.DISPLAY,4.EXIT enter your choice:2 deleted item is:13

1.INSERT,2.DELETE,3.DISPLAY,4.EXIT enter your choice:3 10 14 15 1.INSERT,2.DELETE,3.DISPLAY,4.EXIT enter your choice:2 deleted item is:14

1.INSERT,2.DELETE,3.DISPLAY,4.EXIT enter your choice:3 10 15 1.INSERT,2.DELETE,3.DISPLAY,4.EXIT enter your choice:2 deleted item is:15

1.INSERT,2.DELETE,3.DISPLAY,4.EXIT enter your choice:3 10

1.INSERT,2.DELETE,3.DISPLAY,4.EXIT enter your choice:2 deleted item is:10

1.INSERT,2.DELETE,3.DISPLAY,4.EXIT enter your choice:2 queue is empty 1.INSERT,2.DELETE,3.DISPLAY,4.EXIT enter your choice:4

/* PROGRAM TO IMPLEMENT ALL THE DEQUEUE OPERATIONS USING ARRAYS */

#include<stdio.h>#define MAX 5int q[MAX],front=0,rear=-1,item;void insrear();void insfront();

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 66: C PROGRAMS

void delfront();void delrear();void display();main(){ int ch=1; clrscr(); while(ch!=6) { printf("\n 1.INSERT AT REAR\t2.INSERT AT FRONT\n3.DELETE AT FRONT\t4.DELETE AT REAR\n5.DISPLAY\t6.EXIT\n"); fflush(stdin); printf("enter your choice:"); scanf("%d",&ch); switch(ch) {

case 1:insrear(); break;

case 2:insfront(); break;

case 3:delfront(); break;

case 4:delrear(); break;

case 5:display(); break;

case 6:exit(0);

default:printf("INVALID CHOICE\n"); }

} }

void insrear() { if(rear==MAX-1)

printf("DeQueue is full\n"); else

{ printf("enter item\n"); scanf("%d",&item); rear++; q[rear]=item; }

}

void insfront() { if(front==0&&rear==MAX-1) printf("DeQueue is full\n"); else {

printf("enter item\n");scanf("%d",&item);front--;q[front]=item;

} }

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 67: C PROGRAMS

void delfront() { if(rear==-1) printf("DeQueue ic empty"); item=q[front]; if(front!=rear) front++; else { front=0; rear=-1; } printf("\n Deleted item is:%d\n",item); }

void delrear() { if(rear==-1)

printf("DeQueue is empty"); item=q[rear];

if(front!=rear) rear--;

else {

front=0; rear=-1;

} printf("\n Deleted item is:%d\n",item); }

void display() { int i; if((rear==-1)&&(front==0))

printf("DeQueue is empty"); else { for(i=front;i<=rear;i++) printf("%4d",q[i]); printf("\n"); } }

OUTPUT:

1.INSERT AT REAR 2.INSERT AT FRONT3.DELETE AT FRONT 4.DELETE AT REAR5.DISPLAY 6.EXITenter your choice:2enter item33

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 68: C PROGRAMS

1.INSERT AT REAR 2.INSERT AT FRONT3.DELETE AT FRONT 4.DELETE AT REAR5.DISPLAY 6.EXITenter your choice:1enter item22

1.INSERT AT REAR 2.INSERT AT FRONT3.DELETE AT FRONT 4.DELETE AT REAR5.DISPLAY 6.EXITenter your choice:5 33 22

1.INSERT AT REAR 2.INSERT AT FRONT3.DELETE AT FRONT 4.DELETE AT REAR5.DISPLAY 6.EXITenter your choice:1Enter item44

1.INSERT AT REAR 2.INSERT AT FRONT3.DELETE AT FRONT 4.DELETE AT REAR5.DISPLAY 6.EXITenter your choice:5 33 22 44

1.INSERT AT REAR 2.INSERT AT FRONT3.DELETE AT FRONT 4.DELETE AT REAR5.DISPLAY 6.EXITenter your choice:3

Deleted item is:33

1.INSERT AT REAR 2.INSERT AT FRONT3.DELETE AT FRONT 4.DELETE AT REAR5.DISPLAY 6.EXITenter your choice:5 22 44

1.INSERT AT REAR 2.INSERT AT FRONT3.DELETE AT FRONT 4.DELETE AT REAR5.DISPLAY 6.EXITenter your choice:4

Deleted item is:44

1.INSERT AT REAR 2.INSERT AT FRONT3.DELETE AT FRONT 4.DELETE AT REAR5.DISPLAY 6.EXITenter your choice:5 22

1.INSERT AT REAR 2.INSERT AT FRONT3.DELETE AT FRONT 4.DELETE AT REAR5.DISPLAY 6.EXITenter your choice:3

Deleted item is:22

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 69: C PROGRAMS

1.INSERT AT REAR 2.INSERT AT FRONT3.DELETE AT FRONT 4.DELETE AT REAR5.DISPLAY 6.EXITenter your choice:5DeQueue is empty 1.INSERT AT REAR 2.INSERT AT FRONT3.DELETE AT FRONT 4.DELETE AT REAR5.DISPLAY 6.EXITenter your choice:6

/* PROGRAM TO DEMONSTRATE ALL OPERATIONS ON A SINGLE LINKED LIST */

#include<stdio.h>#include<conio.h>#include<stdlib.h>int count=0;struct node{ int data; struct node *next;}*first=NULL,*last=NULL,*temp;

void create();void delete();void delbeg();void delend();void insert();

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 70: C PROGRAMS

void insbeg();void insend();void search();void display();

main(){ int ch; clrscr(); printf("1.Creation 2.Deletion 3.Insertion 4.Searching 5.Display"); printf("\nEnter choice for linked list operation:"); scanf("%d",&ch); do { switch(ch) { case 1:create();

break; case 2:delete();

break; case 3:insert();

break; case 4:search();

break; case 5:display();

break; } printf("\nEnter choice for Linked list operation:"); scanf("%d",&ch); } while((ch>=1)&&(ch<=5)); getch();}

void create(){ struct node *newnode=(struct node *)malloc(sizeof(struct node)); printf("\nEnter data to insert:"); scanf("%d",&newnode->data); newnode->next=NULL; if(first==NULL) first=newnode; else last->next=newnode; last=newnode; count++;}

void delete(){ int ch,pos,i; if(first==NULL) printf("\nThe List is empty"); else { printf("\nFor deleting first node press 1"); printf("\nfor deleting end node press 2"); printf("\nFor any other node press 3"); printf("\n\nEnter choice to delete:");

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 71: C PROGRAMS

scanf("%d",&ch); if(ch==1) delbeg(); else if(ch==2) delend(); else if(ch==3) { printf("\nEnter the node position to delete:"); scanf("%d",&pos); temp=first; for(i=1;i<=pos-2;i++)

temp=temp->next; if(temp->next!=NULL) {

printf("\nThe deleted element=%d",temp->next->data); temp->next=temp->next->next; count--;

} else

printf("\nThe entered node position is not present in the Linked list:"); } }}void delbeg(){printf("\n the deleted element is %d",first->data);first=first->next;count--;}void delend(){ printf("\nThe deleted element=%d",last->data); temp=first; if(temp==last) first=last=NULL; else { while(temp->next!=last) temp=temp->next; temp->next=NULL; last=temp; } count--;}

void insert(){ int ch,pos,i; printf("\nFor insertion at beginning press 1"); printf("\nFor insertion at end press 2"); printf("\nFor any other position press 3"); printf("\nEnter choice where u want to insert:"); scanf("%d",&ch); if(ch==1) insbeg(); else if(ch==2)

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 72: C PROGRAMS

insend(); else if(ch==3) { struct node *newnode=(struct node*)malloc(sizeof(struct node)); printf("\nEnter the data to insert for newnode:"); scanf("%d",&newnode->data); printf("\nenter the position where u want to insert a newnode:"); scanf("%d",&pos); temp=first; for(i=1;i<pos-2;i++)

temp=temp->next; newnode->next=temp->next; temp->next=newnode; count++; }}

void insbeg(){ struct node *newnode=(struct node*)malloc(sizeof(struct node)); printf("\nEnter the data for newnode:"); scanf("%d",&newnode->data); newnode->next=first; first=newnode; count++;}

void insend(){ struct node *newnode=(struct node*)malloc(sizeof(struct node)); printf("\nEnter the data for newnode:"); scanf("%d",&newnode->data); last->next=newnode; newnode->next=NULL; last=newnode; count++;}

void search(){ int pos=0,e; if(first==NULL) printf("\nList is empty:"); else { printf("\nEnter the element which u want to search:"); scanf("%d",&e); temp=first; while((temp->next!=NULL)&&(temp->data!=e)) { temp=temp->next; pos++; } if(temp->data==e) printf("\nElement %d is found in the position %d",e,pos+1); else printf("\nElement not found"); }

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 73: C PROGRAMS

}

void display(){ if(first==NULL) printf("\nThe list is empty:"); else { temp=first; while(temp->next!=NULL) { printf("%d->",temp->data); temp=temp->next; } printf("%d",temp->data); } printf("\n");}

OUTPUT:

1.Creation 2.Deletion 3.Insertion 4.Searching 5.DisplayEnter choice for linked list operation:5The list is empty:Enter choice for Linked list operation:1Enter data to insert:11

Enter choice for Linked list operation:1Enter data to insert:22

Enter choice for Linked list operation:1Enter data to insert:33

Enter choice for Linked list operation:511->22->33

Enter choice for Linked list operation:3For insertion at beginning press 1For insertion at end press 2For any other position press 3Enter choice where u want to insert:1Enter the data for newnode:5

Enter choice for Linked list operation:55->11->22->33

Enter choice for Linked list operation:3For insertion at beginning press 1For insertion at end press 2For any other position press 3Enter choice where u want to insert:2Enter the data for newnode:44

Enter choice for Linked list operation:3For insertion at beginning press 1For insertion at end press 2For any other position press 3

Enter choice where u want to insert:2Enter the data for newnode:44

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 74: C PROGRAMS

Enter choice for Linked list operation:55->11->22->33->44->44

Enter choice for Linked list operation:3For insertion at beginning press 1For insertion at end press 2For any other position press 3Enter choice where u want to insert:3Enter the data to insert for newnode:25

enter the position where u want to insert a newnode:4Enter choice for Linked list operation:55->11->25->22->33->44->44

Enter choice for Linked list operation:2For deleting first node press 1for deleting end node press 2For any other node press 3

Enter choice to delete:1 the deleted element is 5Enter choice for Linked list operation:511->25->22->33->44->44

Enter choice for Linked list operation:2For deleting first node press 1for deleting end node press 2For any other node press 3

Enter choice to delete:1 the deleted element is 5Enter choice for Linked list operation:511->25->22->33->44->44

Enter choice for Linked list operation:2For deleting first node press 1for deleting end node press 2For any other node press 3Enter choice to delete:1

the deleted element is 11Enter choice for Linked list operation:525->22->33->44->44

Enter choice for Linked list operation:2

For deleting first node press 1for deleting end node press 2For any other node press 3

Enter choice to delete:2

The deleted element=44Enter choice for Linked list operation:525->22->33->44

Enter choice for Linked list operation:2For deleting first node press 1

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 75: C PROGRAMS

for deleting end node press 2For any other node press 3Enter choice to delete:3Enter the node position to delete:3

The deleted element=33Enter choice for Linked list operation:525->22->44

Enter choice for Linked list operation:4Enter the element which u want to search:11

Element not foundEnter choice for Linked list operation:4

Enter the element which u want to search:22

Element 22 is found in the position 2Enter choice for Linked list operation:525->22->44

Enter choice for Linked list operation:6

/* PROGRAM TO IMPLEMENT ALL THE STACK OPERATIONS USING SINGLE LINKED LIST */

#include<stdio.h>#include<stdlib.h>#define NULL 0struct stack{ int data; struct stack *addr; };typedef struct stack node;node *top,*new;main(){ int i; clrscr(); top=NULL; while(1) { printf("1.PUSH,2.POP3,DISPLAY,4.EXIT\n"); printf("enter your choice : "); scanf("%d",&i); switch(i) { case 1:new=(node *)malloc(sizeof(node));

if(new==NULL) { printf("the stack is overflow\n"); break; } else { printf(" enter data\n");

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 76: C PROGRAMS

scanf("%d",&new->data); new->addr=top; top=new; } break;

case 2:if(top==NULL) { printf("the stack is underflow\n"); break; } else { new=top; top=top->addr; printf(" the element popped is%d\n",new->data); new->addr=NULL; free(new); } break;

case 3:if(top==NULL) { printf(" the stack is empty\n"); break; } else { new=top; printf("%d",top->data); do {

new=new->addr;printf("->%d\n",new->data);}while(new->addr!=NULL);

} break;

case 4:exit(0); }}

}

OUTPUT :

1.PUSH,2.POP3,DISPLAY,4.EXITenter your choice : 1 enter data101.PUSH,2.POP3,DISPLAY,4.EXITenter your choice : 1 enter data201.PUSH,2.POP3,DISPLAY,4.EXITenter your choice : 320->101.PUSH,2.POP3,DISPLAY,4.EXITenter your choice : 1 enter data301.PUSH,2.POP3,DISPLAY,4.EXIT

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 77: C PROGRAMS

enter your choice : 320->101.PUSH,2.POP3,DISPLAY,4.EXITenter your choice : 1 enter data301.PUSH,2.POP3,DISPLAY,4.EXITenter your choice : 330->20->101.PUSH,2.POP3,DISPLAY,4.EXITenter your choice : 2 the element popped is301.PUSH,2.POP3,DISPLAY,4.EXITenter your choice : 320->101.PUSH,2.POP3,DISPLAY,4.EXITenter your choice : 4

/* PROGRAM TO IMPLEMENT ALL THE QUEUE OPERATIONS USING SINGLE LINKED LIST */

#include<stdio.h>#include<stdlib.h>struct queue { int data; struct queue*addr; }; typedef struct queue node; node*front,*rear,*new;

main(){int ch;clrscr();front=NULL;rear=NULL;while(1) { printf("1.INSERT 2.DELETE 3.DISPLAY 4.EXIT\n"); scanf("%d",&ch); switch(ch) { case 1:new=(node*)malloc(sizeof(node));

if(new==NULL)printf("\nOverflow\n");else { printf("Enter data\n"); scanf("%d",&new->data); new->addr=NULL; if(front==NULL&&rear==NULL) front=rear=new; else { rear->addr=new; rear=new; }

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 78: C PROGRAMS

}break;

case 2:if(front==NULL)printf("\nThe queue is underflow\n");else { new=front; front=front->addr; printf("The deleted element is %d\n",new->data); new->addr=NULL; free(new); }break;

case 3:if(front==NULL)printf("Queue is empty\n");else { printf("The Queue is\n"); new=front; printf("%d\t",new->data); do { new=new->addr; printf("%d\t",new->data); }while(new->addr!=NULL); printf("\n"); }break;

case 4:exit(0); } }}

OUTPUT :

1.INSERT 2.DELETE 3.DISPLAY 4.EXIT3Queue is empty1.INSERT 2.DELETE 3.DISPLAY 4.EXIT1Enter data221.INSERT 2.DELETE 3.DISPLAY 4.EXIT1Enter data341.INSERT 2.DELETE 3.DISPLAY 4.EXIT1Enter data401.INSERT 2.DELETE 3.DISPLAY 4.EXIT3The Queue is22 34 401.INSERT 2.DELETE 3.DISPLAY 4.EXIT2The deleted element is 221.INSERT 2.DELETE 3.DISPLAY 4.EXIT3The Queue is

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 79: C PROGRAMS

34 401.INSERT 2.DELETE 3.DISPLAY 4.EXIT2The deleted element is 341.INSERT 2.DELETE 3.DISPLAY 4.EXIT2The deleted element is 401.INSERT 2.DELETE 3.DISPLAY 4.EXIT2

The queue is underflow1.INSERT 2.DELETE 3.DISPLAY 4.EXIT4

/* PROGRAM TO DEMONSTRATE ALL OPERATIONS ON A DOUBLE LINKED LIST */

#include<stdio.h>#include<conio.h>#include<stdlib.h>int count=0;struct node{ int data; struct node *prev; struct node *next;}*first=NULL,*last=NULL,*temp;void create();void delete();void delbeg();void delend();void insert();void insbeg();void insend();void search();void display();main(){ int ch; clrscr(); printf("1.creation 2.deletion 3.insertion 4.searching 5.display"); printf("\nEnter choice for linked list operation: "); scanf("%d",&ch); do { switch(ch) { case 1:create();

break; case 2:delete();

break; case 3:insert();

break; case 4:search();

break; case 5:display();

break; }

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 80: C PROGRAMS

printf("\nEnter the choice for linked list operation: "); scanf("%d",&ch); } while((ch>=1)&&(ch<=5)); getch();}void create(){struct node *newnode=(struct node *)malloc(sizeof(struct node));printf("\nEnter data to insert:");scanf("%d",&newnode->data);newnode->next=NULL;if(first==NULL){first=newnode;newnode->prev=NULL;}else{last->next=newnode;newnode->prev=last;}last=newnode;count++;}void delete(){int ch,pos,i;if(first==NULL)

printf("\nthe list is empty");else{ printf("\nfor deleting first node press 1"); printf("\nfor deleting end node press 2"); printf("\nfor any other node press 3"); printf("\nenter choice to delete:"); scanf("%d",&ch); if(ch==1) delbeg(); else if(ch==2) delend(); else if(ch==3) { printf("\nenterthe node position to delete:"); scanf("%d",&pos); temp=first; for(i=1;i<=pos-2;i++) temp=temp->next; if(temp->next!=NULL) { printf("\n the deleted element=%d",temp->next->data); temp->next->next->prev=temp; temp->next=temp->next->next; count--; } else printf("\nthe entered node position is not present in the double Linked list");

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 81: C PROGRAMS

} }}void delbeg(){ printf("\nThe delted element=%d",first->data); first=first->next; first->prev=NULL; count--;}void delend(){printf("\nThe deleted element=%d",last->data);temp=first; if(temp==last) first=last=NULL; else { while(temp->next!=last) temp=temp->next; temp->next=NULL; last=temp; } count--;}void insert(){int ch,pos,i;printf("\nFor insertion at beginning press 1");printf("\nFor insertion at the end press 2");printf("\nFor any other position press 3");printf("\n\nEnter choice where u want to insert:");scanf("%d",&ch);if(ch==1) insbeg();else if(ch==2) insend(); else if(ch==3) { struct node *newnode=(struct node*)malloc(sizeof(struct node)); printf("\nEnter data to insert for new node :"); scanf("%d",&newnode->data); printf("\nEnter the position where u want to insert a newnode:"); scanf("%d",&pos); temp=first; for(i=1;i<=pos-2;i++) temp=temp->next; newnode->next=temp->next; temp->next->prev=newnode; temp->next=newnode; newnode->prev=temp; count++; }}void insbeg(){struct node *newnode=(struct node*)malloc(sizeof(struct node));

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 82: C PROGRAMS

printf("\nEnter the data for new node:");scanf("%d",&newnode->data);newnode->next=first;newnode->prev=NULL;first->prev=newnode;first=newnode;count++;}void insend(){struct node*newnode=(struct node*)malloc(sizeof(struct node));printf("\nEnter the data for new node:");scanf("%d",&newnode->data);newnode->prev=last;newnode->next=NULL;last->next=newnode;last=newnode;count++;}void search(){int pos=0,e;if(first==NULL) printf("\nlist is empty");else { printf("\nEnter the element which u want to search:"); scanf("%d",&e); temp=first; while((temp->next!=NULL) && (temp->data!=e)) { temp=temp->next; pos++; } if(temp->data==e) printf("\nElement %d is found in the position %d",e,pos+1); else printf("\nElement not found"); }}void display(){if(first==NULL) printf("\nThe list is empty");else{ temp=first; while(temp->next!=NULL) { printf("%d<->",temp->data); temp=temp->next; } printf("%d",temp->data);}}

OUTPUT :

1.creation 2.deletion 3.insertion 4.searching 5.displayEnter choice for linked list operation: 1

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 83: C PROGRAMS

Enter data to insert:22Enter the choice for linked list operation: 522Enter the choice for linked list operation: 3

For insertion at beginning press 1For insertion at the end press 2For any other position press 3Enter choice where u want to insert:1Enter the data for new node:11Enter the choice for linked list operation: 511<->22

Enter the choice for linked list operation: 3For insertion at beginning press 1For insertion at the end press 2For any other position press 3Enter choice where u want to insert:2Enter the data for new node:33Enter the choice for linked list operation: 511<->22<->33

Enter the choice for linked list operation: 3For insertion at beginning press 1For insertion at the end press 2For any other position press 3Enter choice where u want to insert:3Enter data to insert for new node :25Enter the position where u want to insert a newnode:3Enter the choice for linked list operation: 511<->22<->25<->33

Enter the choice for linked list operation: 4Enter the element which u want to search:25Element 25 is found in the position 3Enter the choice for linked list operation: 2

for deleting first node press 1for deleting end node press 2for any other node press 3enter choice to delete:1The delted element=11Enter the choice for linked list operation: 522<->25<->33

Enter the choice for linked list operation: 2for deleting first node press 1for deleting end node press 2for any other node press 3enter choice to delete:2The deleted element=33

Enter the choice for linked list operation: 2for deleting first node press 1for deleting end node press 2for any other node press 3enter choice to delete:3enterthe node position to delete:2

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 84: C PROGRAMS

the deleted element=25Enter the choice for linked list operation: 522Enter the choice for linked list operation: 6

/* PROGRAM TO DEMONSTRATE ALL OPERATIONS ON A CIRCULAR LINKED LIST */

#include<stdio.h>#include<conio.h>#include<stdlib.h>int count=0;struct node{ int data; struct node *next; }*first=NULL,*last=NULL,*temp; void create(); void delete(); void delbeg(); void delend(); void insert(); void insbeg(); void insend(); void search(); void display(); main() { int ch; clrscr(); printf("1.creation,2.deletion,3.insertion,4.searching,5.display"); printf("\n enter choice for circular linked list operation:"); scanf("%d",&ch); do { switch(ch) { case 1:create();

break; case 2:delete();

break; case 3:insert();

break; case 4:search();

break; case 5:display();

break; } printf("\n enter choice for linked list operation:"); scanf("%d",&ch); } while((ch>=1)&&(ch<=5)); getch(); }

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 85: C PROGRAMS

void create() { struct node *newnode=(struct node *)malloc(sizeof(struct node)); printf("\n enter data to insert:"); scanf("%d",&newnode->data); newnode->next=NULL; if(first==NULL) first=newnode; else last->next=newnode; newnode->next=first; last=newnode; count++; }

void delete() { int ch,pos,i; if(first==NULL) printf("\n the circular list is empty"); else { printf("\n for deleting firstnode press1"); printf("\n for deleting end node press2"); printf(" \n for any other node press 3"); printf("\n\nenter choice to delete"); scanf("%d",&ch); if(ch==1)

delbeg(); else if(ch==2)

delend(); else if(ch==3)

{ printf("\nenter the node position to delete"); scanf("%d",&pos); temp=first; for(i=1;i<=pos-2;i++) temp=temp->next; if(temp->next!=NULL) { printf("\n the delted element=%d",temp->next->data); temp->next=temp->next->next; count--; } else printf("\n the entered node position is not present in the linked list"); } } }

void delbeg() { printf("\n the deleted element =%d",first->data); if(first->next==first) first=last=NULL;

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 86: C PROGRAMS

else { first= first->next; last->next=first; count--; } }

void delend() {

printf("\n the deleted element=%d",last->data); temp=first;if(temp->next==first) first=last=NULL; else { while(temp->next!=last) temp=temp->next; temp->next=first; last=temp; } count--; }

void insert() { int ch,pos,i; printf("\n for insertion atbeginning press1"); printf("\nfor insertion at the end press2"); printf("\n for any other position press 3"); printf("\n\n enter u r choice where u want to insert:"); scanf("%d",&ch); if(ch==1) insbeg(); else if(ch==2)

insend(); else if(ch==3) { struct node *newnode=(struct node *)malloc(sizeof(struct node)); printf("\n enter data to insert for new node:"); scanf("%d",&newnode->data); printf("\n enter the position where u want to insert the newnode:"); scanf("%d",&pos); temp=first; for(i=1;i<=pos-2;i++) temp=temp->next; newnode->next=temp->next;

temp->next=newnode; count++; } }

void insbeg() { struct node *newnode=(struct node *)malloc(sizeof(struct node)); printf("\n enter the datafor newnode:"); scanf("%d",&newnode->data);

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 87: C PROGRAMS

newnode->next=first; first=newnode; last->next=first; count++; }

void insend() { struct node *newnode=(struct node *)malloc(sizeof(struct node)); printf("\n enter the data for newnode:"); scanf("%d",&newnode->data); last->next=newnode; newnode->next=first; last=newnode; count++; }

void search() { int pos=0,e; if(first==NULL) printf("\n the circular list is empty"); else { printf("\n enter the element which u want to search:"); scanf("%d",&e); temp=first; while((temp->next!=first)&&(temp->data!=e))

{ temp=temp->next; pos++; }

if(temp->data==e)printf("\nelement %d is found in the position %d",e,pos+1);

elseprintf("\n element not found");}

} void display()

{if(first==NULL) printf("\n the list is empty");else{ temp=first;while(temp->next!=first){ printf("%d->",temp->data); temp=temp->next; }

printf("%d",temp->data); }

}

OUTPUT :

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 88: C PROGRAMS

1.creation,2.deletion,3.insertion,4.searching,5.display enter choice for circular linked list operation:1

enter data to insert:10

enter choice for linked list operation:1

enter data to insert:20

enter choice for linked list operation:510->20 enter choice for linked list operation:3

for insertion atbeginning press1for insertion at the end press2 for any other position press 3

enter u r choice where u want to insert:1

enter the datafor newnode:30

enter choice for linked list operation:530->10->20 enter choice for linked list operation:3

for insertion atbeginning press1for insertion at the end press2 for any other position press 3

enter u r choice where u want to insert:2

enter the data for newnode:40

enter choice for linked list operation:530->10->20->40 enter choice for linked list operation:3

for insertion atbeginning press1for insertion at the end press2 for any other position press 3

enter u r choice where u want to insert:3

enter data to insert for new node:50

enter the position where u want to insert the newnode:2

enter choice for linked list operation:530->50->10->20->40 enter choice for linked list operation:4

enter the element which u want to search:60

element not found enter choice for linked list operation:4

enter the element which u want to search:10

element 10 is found in the position 3

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 89: C PROGRAMS

enter choice for linked list operation:530->50->10->20->40 enter choice for linked list operation:2

for deleting firstnode press1 for deleting end node press2 for any other node press 3

enter choice to delete1

the deleted element =30 enter choice for linked list operation:550->10->20->40 enter choice for linked list operation:2

for deleting firstnode press1 for deleting end node press2 for any other node press 3

enter choice to delete2

the deleted element=40 enter choice for linked list operation:550->10->20 enter choice for linked list operation:2

for deleting firstnode press1 for deleting end node press2 for any other node press 3

enter choice to delete3

enter the node position to delete 2

the delted element=10

enter choice for linked list operation:5

50->20

enter choice for linked list operation:6

/*PROGRAM TO DEMONSTRATE ALL OPERATIONS ON A CIRCULAR DOUBLE LINKED LIST */

#include<stdio.h>#include<conio.h>#include<stdlib.h>int count=0;struct node

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 90: C PROGRAMS

{ int data; struct node *prev; struct node *next;}*first=NULL,*last=NULL,*temp;void create();void delete();void delbeg();void delend();void insert();void insbeg();void insend();void search();void display();main(){ int ch; clrscr(); printf("1.creation 2.deletion 3.insertion 4.searching 5.display"); printf("\nEnter choice for lincked list operation: "); scanf("%d",&ch); do { switch(ch) { case 1:create();

break; case 2:delete();

break; case 3:insert();

break; case 4:search();

break; case 5:display();

break; } printf("\nEnter the choice for linked list operation: "); scanf("%d",&ch); } while((ch>=1)&&(ch<=5)); getch();}void create(){struct node *newnode=(struct node *)malloc(sizeof(struct node));printf("\nEnter data to insert:");scanf("%d",&newnode->data);newnode->next=NULL;if(first==NULL){first=newnode;newnode->prev=NULL;}else{last->next=newnode;newnode->prev=last;}newnode->next=first;

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 91: C PROGRAMS

last=newnode;count++;}void delete(){int ch,pos,i;if(first==NULL)

printf("\nthe list is empty");else{ printf("\nfor deleting first node press 1"); printf("\nfor deleting end node press 2"); printf("\nfor any other node press 3"); printf("\nenter choice to delete:"); scanf("%d",&ch); if(ch==1) delbeg(); else if(ch==2) delend(); else if(ch==3) { printf("\nenterthe node position to delete:"); scanf("%d",&pos); temp=first; for(i=1;i<=pos-2;i++) temp=temp->next; if(temp->next!=NULL) { printf("\n the deleted element=%d",temp->next->data); temp->next=temp->next->next; count--; } else printf("\nthe entered node position is not present in the double Linked list"); } }}void delbeg(){printf("\nThe delted element=%d",first->data);if(first->next==first) first=last=NULL;else{ first=first->next; first->prev=NULL; last->next=first; count--; }}void delend(){temp=first;printf("\nThe deleted element=%d",last->data); if(temp==last) first=last=NULL; else

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 92: C PROGRAMS

{ while(temp->next!=last) temp=temp->next; temp->next=first; last=temp; } count--;}void insert(){int ch,pos,i;printf("\nFor insertion at beginning press 1");printf("\nFor insertion at the end press 2");printf("\nFor any other position press 3");printf("\n\nEnter choice where u want to insert:");scanf("%d",&ch);if(ch==1) insbeg();else if(ch==2) insend(); else if(ch==3) { struct node*newnode=(struct node*)malloc(sizeof(struct node)); printf("\nEnter data to insert for new node :"); scanf("%d",&newnode->data); printf("\nEnter the position where u want to insert a newnode:"); scanf("%d",&pos); temp=first; for(i=1;i<=pos-2;i++) temp=temp->next; temp->next->prev=newnode; newnode->next=temp->next; temp->next=newnode; newnode->prev=temp; count++; }}void insbeg(){struct node*newnode=(struct node*)malloc(sizeof(struct node));printf("\nEnter the data for new node:");scanf("%d",&newnode->data);newnode->next=first;newnode->prev=NULL;first->prev=newnode;first=newnode;last->next=first;count++;}void insend(){struct node*newnode=(struct node*)malloc(sizeof(struct node));printf("\nEnter the data for new node:");scanf("%d",&newnode->data);newnode->prev=last;newnode->next=first;last->next=newnode;

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 93: C PROGRAMS

last=newnode;count++;}void search(){int pos=0,e;if(first==NULL) printf("\nCircular list is empty");else { printf("\nEnter the element which u want to search:"); scanf("%d",&e); temp=first; while((temp->next!=first) && (temp->data!=e)) { temp=temp->next; pos++; } if(temp->data==e) printf("\nElement %d is found in the position %d",e,pos+1); else printf("\nElement not found"); }}void display(){if(first==NULL) printf("\nThe list is empty");else{ temp=first; while(temp->next!=first) { printf("%d<->",temp->data); temp=temp->next; } printf("%d",temp->data);}}

OUTPUT :

1.creation 2.deletion 3.insertion 4.searching 5.display

Enter choice for lincked list operation: 1Enter data to insert:10

Enter the choice for linked list operation: 1Enter data to insert:20

Enter the choice for linked list operation: 510<->20

Enter the choice for linked list operation: 3

For insertion at beginning press 1

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 94: C PROGRAMS

For insertion at the end press 2For any other position press 3

Enter choice where u want to insert:1Enter the data for new node:30

Enter the choice for linked list operation: 530<->10<->20

Enter the choice for linked list operation: 3

For insertion at beginning press 1For insertion at the end press 2For any other position press 3

Enter choice where u want to insert:2Enter the data for new node:40

Enter the choice for linked list operation: 530<->10<->20<->40

Enter the choice for linked list operation: 3

For insertion at beginning press 1For insertion at the end press 2For any other position press 3

Enter choice where u want to insert:3Enter data to insert for new node :50

Enter the position where u want to insert a newnode:2Enter the choice for linked list operation: 530<->50<->10<->20<->40

Enter the choice for linked list operation: 4Enter the element which u want to search:60

Element not foundEnter the choice for linked list operation: 4Enter the element which u want to search:50

Element 50 is found in the position 2Enter the choice for linked list operation: 2

for deleting first node press 1for deleting end node press 2for any other node press 3

enter choice to delete:1The delted element=30

Enter the choice for linked list operation: 550<->10<->20<->40

Enter the choice for linked list operation: 2

for deleting first node press 1for deleting end node press 2for any other node press 3

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 95: C PROGRAMS

enter choice to delete:2The deleted element=40

Enter the choice for linked list operation: 550<->10<->20

Enter the choice for linked list operation: 2

for deleting first node press 1for deleting end node press 2for any other node press 3

enter choice to delete:3enter the node position to delete:2 the deleted element=10Enter the choice for linked list operation: 550<->20

Enter the choice for linked list operation: 6

/* PROGRAM ON BUBBLE SORT */

#include<stdio.h>main(){ int a[10],i,j,n,temp; clrscr(); printf("enter the value of n:\n"); scanf("%d",&n); printf("enter the elements to be sorted:\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n;i++) { for(j=0;j<n-1-i;j++) { if(a[j]>a[j+1]) {

temp=a[j];a[j]=a[j+1];a[j+1]=temp;

}

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 96: C PROGRAMS

} } printf("the sorted elements are:\n"); printf("*********************\n"); for(i=0;i<n;i++) printf("%d\n",a[i]); printf("*********************\n"); getch();}

OUTPUT :

enter the value of n: 6enter the elements to be sorted:32126585982the sorted elements are:*********************91232658285*********************

/* PROGRAM TO IMPLEMENT SELECTION SORT USIMG AN ARRAY */

#include<stdio.h>main(){ int a[100],i,n; clrscr(); printf("enter the limit\n"); scanf("%d",&n); printf("\n enter the elements\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); ssort(a,n); printf("\n the sorted data of the array is\n"); for(i=0;i<n;i++) printf("\n%3d",a[i]); getch();}/** selection sort function**/ssort(int a[],int n){ int i,j,k,small; for(i=0;i<n-1;i++)

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 97: C PROGRAMS

{ small=i; for(j=i+1;j<n;j++) if(a[small]>a[j]) small=j; k=a[i]; a[i]=a[small]; a[small]=k; }}

OUTPUT :

enter the limit5

enter the elements3678894

the sorted data of the array is

3 4 6 78 89

/* PROGRAM TO IMPLEMENT INSERTION SORT USIMG AN ARRAY */

#include<stdio.h>main(){ int a[100],i,n; clrscr(); printf("enter the limit\n"); scanf("%d",&n); printf("\n enter the elements\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); isort(a,n); printf("\n the sorted data of the array is\n"); for(i=0;i<n;i++) printf("\n%3d",a[i]); getch();}/** insertion sort function**/isort(int a[],int n){ int i,pos,temp; for(i=0;i<n-1;i++) { pos=i;

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 98: C PROGRAMS

temp=a[i]; while((pos|=0)&&(a[pos-1]>temp)) { a[pos]=a[pos-1]; --pos; } a[pos]=temp;}}

OUTPUT :

enter the limit5

enter the elements82968995101

the sorted data of the array is

82 89 95 96 101

/* PROGRAM TO IMPLEMENT QUICK SORT USIMG AN ARRAY */

#include<stdio.h>main(){ int a[100],i,n; clrscr(); printf("enter the limit\n"); scanf("%d",&n); printf("\n enter the elements\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("\nbefore sort\n"); for(i=0;i<n;i++) printf("%d\n",a[i]); quicksort(a,0,n-1); printf("\n after sort\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("\n before sort\n"); for(i=0;i<n;i++) printf("%d\n",a[i]); quicksort(a,0,n-1);

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 99: C PROGRAMS

printf("\n after sort\n"); for(i=0;i<n;i++) printf("\n%d\n",a[i]); getch(); }quicksort(int a[],int lb,int ub){ int i,p,temp,j; if(lb<=ub) { i=p=lb; j=ub; while(i<j) { while((a[i]<=a[p])&&(i<ub)) { i++;}while(a[j]>a[p]){j--;}if(i<j) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } temp=a[p]; a[p]=a[j]; a[j]=temp; quicksort(a,lb,j-1); quicksort(a,j+1,ub); }}

OUTPUT :

ENTER LIMIT : 4Enter ELEMENTS : 4312BEFORE SORT4312

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 100: C PROGRAMS

AFTER SORT1234

/* PROGRAM TO IMPLEMENT HEAP SORT USIMG AN ARRAY */

#include<stdio.h>main(){ int a[100],i,n,j; clrscr(); printf("enter the limit\n"); scanf("%d",&n); printf("\n enter the elements\n"); for(i=1;i<=n;i++) scanf("%d",&a[i]); printf("\nbefore sort\n"); for(i=1;i<=n;i++) printf("%d\n",a[i]); heapsort(a,n); printf("\n after sort\n"); for(i=1;i<=n;i++) printf("%d\n",a[i]); getche(); }heapsort(int a[],int n){ int i,temp; creatheap(a,n); printf("\n after heap\n"); for(i=1;i<=n;i++) printf("%d\n",a[i]); for(i=n;i>=1;i--) { temp=a[1]; a[1]=a[i];

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 101: C PROGRAMS

a[i]=temp; reheap(a,i);}}creatheap(int a[],int n){int i,k,j,item;for(k=2;k<=n;k++){j=k;item=a[j];i=(int)j/2;while((i>0)&&(item>a[i])){a[j]=a[i];j=i;i=(int)j/2;}a[j]=item;}}reheap(int a[],int n){int i,j=1,item;item=a[j];i=2*j;while(i<n){if(i+1<n)if(a[i]<a[i+1])i++;if(item<a[i]){a[j]=a[i];j=i;i=2*j;}elsebreak;}a[j]=item;}

OUTPUT :

enter the limit 5 enter the elements1695282before sort169

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 102: C PROGRAMS

5282 after heap8252619 after sort1695282

/* PROGRAM FOR LINEAR SEARCH OF AN KEY ELEMENT FROM A GIVEN LIST OF ELEMENTS */

# include<stdio.h>main(){ int a[10],i,n,key; clrscr(); printf("ENTER THE VALUE OF n :"); scanf ("%d",&n); printf ("ENTER ARRAY ELEMENTS \n"); for(i=0;i<n;i++)

scanf("%d",&a[i]); printf("ENTER THE KEY ELEMENT TO BE SEARCHED : "); scanf("%d",&key); for(i=0;i<n;i++) { if(key==a[i])

{ printf ("SEARCH IS SUCESSFUL \n"); printf ("ELEMENT IS FOUND AT POSITION %d ",i+1); getch(); exit(0);}

} printf ("SEARCH IS UNSUCESSFUL : \n"); printf ("THE ELEMENT IS NOT FOUND"); getch();}

OUTPUT :

M V B REDDY GITAM UNIVERSITY BANGALORE

Page 103: C PROGRAMS

ENTER THE VALUE OF n :5ENTER ARRAY ELEMENTS13862ENTER THE KEY ELEMENT TO BE SEARCHED : 3SEARCH IS SUCESSFULELEMENT IS FOUND AT POSITION 2

M V B REDDY GITAM UNIVERSITY BANGALORE