Vous êtes sur la page 1sur 71

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

INDEX

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

*
*
*
**
35

program to implement addition of two numbers


program to implement area of rectangle
program to find average of three numbers
program to implement simple interest
program to implement Celsius of Fahrenheit temperature
program to implement swapping two numbers
program to implement Arithmetic operators
write a program using increment and decrement operators
program to implement bitwise operators
program to implement logical operators
write a program using conditional operator
program to implement assignment operator
program to implement type casting
write a program to convert into lowercase or uppercase
write a program to find greatest of 3 numbers
write a program to find even or odd number using if else statement
write a program to find discount using if statement
write a program to implement quadratic equations
write a program to find avg. of marks with if else if ladder
program to implement enumerators
program to implement typedef
write a program using switch statement
write a program to know vowel or consonant using switch statement
program to implement Joto statement
program to implement factorial of a number
write a program to implement reverse number
write a program to find sum of natural numbers
write a program to find Armstrong number
write a program to find factor of a no.
write a program to find reverse of a number
program to implement gcd and lcm
write a program to implement fibbonacci series
write a program to implement Pascal triangle
write a program to print format

write a program to print format.

1
12
1
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

123
1234

2
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

36

SVS GROUP OF INSTITUTIONS

write a program to print format

1
22
333
4444
37

1
23
345
4567
38

1
2
3 3
4 4
5 5
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

write a program to print format

write a program to print format

3
4 4
5 5 5
write a program
write a program
write a program
write a program
write a program
write a program
write a program
write a program
matrix
write a program
write a program
write a program
write a program
write a program
write a program
write a program
write a program
write a program
write a program
write a program
write a program
write a program
write a program
write a program
write a program
write a program

using continue statement


to find a prime no.
to explain the one dimensional arrays
to find the biggest element in the array
to read and print two dimensional arrays
to find addition of two matrices
to print multiplication of two matrices
to implement upper triangular and lower triangular
to
to
to
to
to
to
to
to
to
to
to
to
to
to
to
to
to

implement symmetric matrix


implement Diagonal matrix
implement combining two strings
implement comparison of two strings
implement copying string into another
implement palindrome
implement reverse string
implement sorting of strings
implement string handling functions
explain categories of functions
implement recursion
explain Automatic variables
explain external variable
explain static storage class
explain register storage class
implement call by value
implement call by reference
3
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

SVS GROUP OF INSTITUTIONS

write a program find the addition of two matrices using pointers


write a program to implement array of pointers
write a program to implement pointer to an array
write a program to implement pointers and functions
write a program to implement pointer expression
write a program to implement array elements using ptr
write a program to implement sum of array elements using ptrs
write a program to print no.s in reverse order using pointers
write a program to explain pointers in one dimensional array
write a program to implement pointers and character strings
write a program to implement pointers to structures
write a program to implement program to illustrate files
write a program to print sname,course,fee using files
write a program to read data from student file
write a program to write text into the file
write a program to write text into file using fputs
program to read text from file using fgets()
write a program to implement fseek function
write a progrm using ftell and fseek functions
write a program to implement error handling in file operations
Write a program to explain commandline arguments
write a program to implement malloc function
write a program to implement calloc function
write a program to implement stack
write a program to implement stack using pointers
write a program to implement queue
write a program to implement queue using pointers
write a program to implement postfix expression
write a program to evaluate infix to postfix expression
write a program to implement graphs
write a program to implement Linear Search
write a program to implement Binary Search
write a program to implement Bubble sort
write a program to implement Selection sort
write a program to implement quick sort
write a program to implement insertion sort
write a program to implement heap sort

4
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

1.program to implement addition of two numbers


#include<stdio.h>
main()
{
int a,b,c;
printf("enter the numbers");
scanf("%d%d",&a,&b);
c=a+b;
printf("%d",c);
}

2.program to implement area of rectangle


#include<stdio.h>
main()
{
float l=5.6,b=6.8,a;
a=l*b;
printf(the area is %fa);
}

3.program to find average of three numbers


#include<stdio.h>
main()
{
int a,b,c,sum;
float avg;
a=40,b=70,c=80;
sum=a+b+c;
sum=sum/3;
printf(%f,avg);
}

4. program to implement simple interest


#include<stdiop.h>
main()
{
float p,t,r,s;
printf(enter the valuesp,t,r\n);
scanf(%f%f%f,&p,&t,&r);
s=(p*t*r)/100;
printf(the simple interest is %f,s);
}

5
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

5. program to implement celisus of farenheit temprature


#include<stdio.h>
main()
{
float cel,fah;
int icel;
printf("enter temprature in farenheit degrees..");
scanf("%f",&fah);
cel=5*(fah-32.0)/9;
icel=int(cel);
printf("%6.2f degrees of farenhit is equivalent to %6.2f gegrees of
celsius",fah,cel);
printf("truncated celius temprature is %06d",icel);
}

6. program to implement swapping two numbers


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,t;
a=10;
b=20;
printf("a=%d\nb=%d",a,b);
t=a;
a=b;
b=t;
printf("\nafter swapping");
printf("a=%d\nb=%d",a,b);
}

7. program to implement Arithmetic operators


#include<stdio.h>
main()
{
int a,b,c;
printf("enter a,b values");
scanf("%d%d",&a,&b);
c=a+b;
printf("the addition of a,b is %d",c);
c=a-b;
printf("subtraction of a,b is %d",c);
6
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

c=a*b;
printf("the multiplication of a,b is %d",c);
c=a/b;
printf(the result of a/b is %d",c);
c=a%b;
printf("thr result is %d",c);
}

8. wirte a program using increment and decrement operators


#include<stdio.h>
main()
{
int m,m1,m2;
m=10;
m1=10;
++m;
m1++;
printf("m=%d,m1=%d",m,m1);
m2=++m1;
printf("m2=%d,m1=%d",m2,m1):
m2=m++;
printf(m2=%d,m=%d",m2,m);
m2=++m + m++ + ++m;
printf("\n m2=%d,m=%d",m2,m);
}

9. program to implement bitwise operators


#include<stdio.h>
main()
{
int a,b,c;
printf("enter values of ab,c");
scanf("%d%d",&a,&b);
c=a&b;
printf("%d",c);
c=a/b;
printf("%d",c);
c=a<<2;
printf("%d",c);
c=a>>2;
printf("%d",c);
~b;
printf("%d",b);
}
7
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

10. program to implement logical operators


#include<stdio.h>
main()
{
int a=10;
int b=15;
int d=a;
printf("%d\t",a<c);
printf("%d\t",a>=c);
printf("%d\t",a+5==c);
printf("%d\t",!a);
printf("%d\t",!(a<c));
printf("%d\t",a<c&&c<d);
printf("%d\t",a<c&&d<c);
printf("%d\t",a<c||c<d);
}

11.write a program using conditional operator


#include<stdio.h>
main()
{
int n;
printf("enter the value");
scanf("%d",&n);
(n%2==0)?printf("it is even"):printf("it is odd");
}

12. program to implement assignment operator


#include<stdio.h>
main()
{
int a=15,b=10,c;
a+=10;
printf(%d\t,a);
a-=b;
printf(%d\t,a);
c=a*b;
printf(%d\n,c);
c/=2;
printf(%d\t,c);
8
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

13. program to implement type casting


#include<stdio.h>
main()
{
int a=10;
int b=7;
float c=a/b;
printf("c=%f",c);
c=(float)a/b;
printf("c=%f",c);
}

14. write a program to convert into lowercase or uppercase


#include<stdio.h>
#include<ctype.h>
main()
{
char ch,ch1;
printf(enter a lower case letter\n);
ch=getchar();
ch1=toupper(ch);
putchar(ch1);
printf(enter upper case letter\n);
ch=getchar();
putchar(tolower(ch));
}

15. write a program to find greatest of 3 numbers


#include<stdio.h>
main()
{
int a,b,c;
printf("enter 3 values");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
if(a>c)
printf("the largest no. is%d",a);
else
printf("the largest no. is %d",c);
}
else
{
9
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

if(b>c)
printf("the largest no. is %d",b);
else
printf("the largest no. is %d",c);
}
}

16. write a program to find even or odd number using if else


statement
#include<stdio.h>
#include<conio.h>
void main()
{
int a,c;
printf("enter a value ");
scanf("%d",&a);
c=a%2;
if(c==0)
{
printf("number is even");
}
else
printf("number is odd");
}

17. write a program to find discount using if statement


#include<stdio.h>
main()
{
int price,qunt,tcost;
printf("enter the qunatity");
scanf(%d",&quan");
printf("enter the price");
scanf("%d",&price);
tcost=price*quan;
if(tcost>1000)
{
printf("you got offer\n");
tcost=tcost-tcost*10/100;
}
printf("total cost is %d",tcost);
10
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

18.write a program to implement quadratic equations


#include<stdio.h>
#include<math.h>
#include<conio.h>
#include<process.h>
void main()
{
float a,b,c,root1,root2,root,ip,rp,disc;
printf("Enter the co-efficients of the quadratic equation\n\a");
scanf("%f%f%f",&a,&b,&c);
if(a*b*c==0)
{

printf("Equation is not possible\n\a");


exit(0);

printf("Equation is possible\n\a");
disc=(b*b)-(4*a*c);
if(disc==0)
{
printf("Roots are real and equal\n\a");
root=-b/(2*a);
printf("Roots are %f and %f\n\a",root,root);
}
else if(disc>0)
{
printf("Roots are real\n\a");
root1=(-b+sqrt(disc))/(2*a);
root2=(-b-sqrt(disc))/(2*a);
printf("Roots are %f and%f\n\a",root1,root2);
}
else
{
printf("Roots are complex\n\a");
rp=-b/2*a;
ip=sqrt((fabs)(disc))/2*a;
11
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

printf("Real part=%f+i%f\nImaginary part=%f+i%f\n",rp,ip,rp,ip);


}

19. write a program to find avg of marks with if else if ladder


#include<stdio.h>
main()
{
int m1,m2,m3,avg;
printf("enter 3 subject marks");
scanf("%d%d%d",&m1,&m2,&m3);
avg=(m1+m2+m3)/3;
if(avg>70)
printf("A grade");
else if(avg>=60)
printf("B grade");
else if(avg>=50)
printf("C grade");
else
printf("FAIL");
}

20. program to implement enumerators


#include<stdio.h>
main()
{
enum colour{red,green,blue};
enum colour c1,c2;
c1=red;
c2=blue;
printf("%d %d",c1,c2);
}

21.program to implement typedef


#include<stdio.h>
main()
{
typedef int marks;
marks m1,m2;
printf("enter 2 marks");
scanf("%d%d",&m1,&m2);
printf("the 2 marks are %d",m1+m2);
12
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

22. write a program using switch statement


#include<stdio.h>
main()
{
int a,b,c;
printf("enter the value");
scanf("%d%d",&a.&b);
printf("enter your chioce");
scanf("%d",&choice);
switch(chioce)
{
case 1:c=a+b;
printf("addition is %d",c);
break;
case 2:c=a-b;
printf("difference is %d",c);
break;
case 3:c=a*b;
printf("multiplication is%d",c);
break;
case 4:c=a/b;
printf("division is %d",c);
break;
case 5:c=a%b;
printf("%d",c);
break;
default:printf("invalid chioce");
}
}

23. write a program to know vowel or consonant using switch


statement
#include<stdio.h>
main()
{
char choice;
printf("enter the chioce");
scanf("%c",chioce);
switch(chioce)
{
case 'a':printf("it is vowel");
13
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

case 'e:'
case 'i':
case 'o':
case 'u'printf"(it is vowel");
break;
default:printf(it is consonant");
}
}

24. program to implement goto statement


#include<stdio.h>
main()
{
int i=2;
x:
printf(%d\n,i);
i=i+2;
if(i<=10)
goto x;
}

25. program to implement factorial of a number


#include<stdio.h>
main()
{
int i=1,n,fact=1;
printf("enter the number");
scanf("%d",&n);
while(i<=n)
{
fact=fact*i;
i++;
}
printf("fact is %d",fact);
}

26. write a program to implement reverse number


#include<stdio.h>
main()
{
int n,trev,r;
rev=0;
printf("enter the value of n");
scanf("5d",&n);
14
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

while(n!=0)
{
r=n%10;
rev=rev*10+r;
n=n/0;
}
printf("the reverse is %d",rev);
}

27.write a program to find sum of natural numbers


#include<stdio.h>
main()
{
int i=1,sum=0,n;
printf("enter the number");
scaf(%d",&n);
while(i<=n)
{
sum=sum+i;
i++;
}
printf("sum is=%d",sum);
}

28. write a program to find armstrong number


#include<stdio.h>
main()
{
int ,rem,sum=0;
int temp;
printf("enter the value");
sacnf("%d",&n);
temp=n;
while(n>0)
{
rem=n%10;
sum=sum+(rem*rem*rem);
n=n/10;
}
if(temp==sum)
{
printf("it is armstrong number");
}
else
15
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

printf("it is not armtrong number");


}

29. write a program to find factor of a no.


#include<stdio.h>
main()
int n,i=1;
printf('enter n value");
scanf("%d",&n);
while(i<=n)
{
if(n%i==0)
printf("%d\n",i);
i++;
}
}

30. write a program to find reverse of a number


#include<stdio.h>
main()
{
int n,trev,r;
rev=0;
printf("enter the value of n");
canf("5d",&n);
while(n!=0)
{
r=n%10;
rev=rev*10+r;
n=n/0;
}
printf("the reverse is %d",rev);
}

31. program to implement gcd and lcm


#include<stdio.h>
#include<conio.h>
void main()
{
int nr,dr,a,b,lcm,r;
printf("Enter the value for a,b\n")
scanf("%d%d",&a,&b);
if(a>=b)
16
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

nr=a;
dr=b;

}
else
{

nr=b;
dr=a;
}
r=nr%dr;
while(r!=0)
{
nr=dr;
dr=r;
r=nr%dr;
}
lcm=(a*b)/dr;
printf("GCD of %d & %d = %d\n",a,b,dr);
printf("LCM of %d & %d = %d\n",a,b,lcm)
}

32. write a program to implement fibbonacci series


#include<stdio.h>
main()
{
int a=0,b=1,c;
int n;
int i;
printf("how many no.s");
scanf("%d",&n);
printf("fibbonacci series");
printf("%d\t%d",a,b);
i=2;
do
{
c=a+b;
b=c;
i++;
}
while(i<n);
17
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

33. write a program to implement pascal triangle


#include<stdio.h>
#include<conio.h>
void main()
{
int a,p,q,r,x;
a=1;
q=0;
printf("input number of rows");
scanf("%d",&p);
printf("%d",p);
printf("\npascal triangle is");
while(q<p)
{
for(r=40-3*q;r>0;--r)
printf(" ");
for(x=0;x<=q;++x)
{
if((x==0)||(q==0))
a=1;
else
a=(a*(q-x+1))/x;
printf("%6d",a);
printf("\n");
++q;
}
}

34.write a program to print format *


**
***
****
#include<stdio.h>
main()
{
int n,i,j;
printf("how many rows");
scanf("%d",&n);
18
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

for(i=1;i<=n;i++)
{
for(j=1;j<=1;j++)
printf("%c",'*');
printf("\n");
}
}

35. write a program to print format. 1


12
123
1234
#include<stdio.h>
main()
{
int n,i,j;
printf("how many rows");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=1;j++)
printf("%d",j);
printf("\n");
}
}

36. write a program to print format 1

22
333
4444

#include<stdio.h>
main()
{
int n,i,j;
printf("how many rows");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=1;j++)
printf("%d",i);
printf("\n");
19
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

}
}

37. write a program to print format 1


23
345
4567
#include<stdio.h>
main()
{
int n,i,j;
printf("how many rows");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<2*i;j++)
printf("%d",j);
printf("\n");
}
}

38. write a program to


1
2 2
3 3
4 4 4
5 5 5

print format

3
4
5 5

#include<stdio.h>
main()
{
int n,i,j;
printf("how many rows");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(k=i;k<n;k++)
printf(" ");
for(j=1;j<=i;j++)
printf("%d"i);
printf("\n");
}
}
20
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

39. write a program using continue statement


#include<stdio.h>
main()
{
int i;
for(i=1;i<=10;i++)
{
if(i==4)
continue;
printf("%d",i);
}
}

40.write a program to find a prime no.


#include<stdio.h>
main()
{
int n,i;
printf("enter ano");
scanf("%d",&n);
for(i=2;i<=n-1;i++)
{
if(n%i==0)
{
printf("it is not prime");
break;
}
}
if(i==n)
printf("it is a prime");
}

41. W A P TO EXPLAIN THE ONE DIMENSIONAL ARRAYS


#include<stdio.h>
void main()
{
int a[5],i,n;
printf(enter the elements\n);
for(i=0;i<5;i++)
scanf(%d,&a[i]);
printf(\n the elements in array are );
for(i=0;i<5;i++)
printf(a[i]=%d,a[i]);
21
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

42. WAP TO FIND THE BIGGEST ELEMENT IN THE ARRAY


#include<stdio.h>
void main()
{
Int a[5],i,n,big;
printf(\n enter the numbers);
for(i=0;i<5;i++)
{
scanf(%d,&a[i]);
}
big=a[0];
for(i=1;i<5;i++)
{
if(a[i]>big)
big=a[i];
Printf(\n the biggest no is %d,big);
}
}

43. WAP TO READ & PRINT THE TWO DIM ARRAY VALUES
#include<stdio.h>
void main()_
{
int a[2][3],i,j;
printf(\n enter the elements );
for(i=0;i<2;i++)
for(j=0;j<3;j++)
{
scanf(%d,&a[i][j]);
}
printf(\n the elements in array );
for(i=0;i<2;i++)
for(j=0;j<3;j++)
22
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

{
printf(%d,a[i][j]);
}
printf(\n);
}

23
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

44. WAP TO FIND ADDITION OF TWO MATRICES


#include<stdio.h>
void main()
{
int a[5][5],b[5][5],c[5][5],i,j,m,n;
printf(\n enter the order of matrix a& b);
scanf(%d%d,&m,&n);
printf(\n enter elements in matrix a);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
scanf(%d,&a[i][j]);
}
printf(enter the elements in matrix b);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
scanf(%d,&b[i][j]);
}
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
printf(enter the resultant matrix c );
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
printf(%3d,c[i][j]);

}
printf(\n);
}
}

45. WAP TO PRINT MULTIPLICATION OF TWO MATRICES


#include<stdio.h>
void main()
{
24
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

int a[3][3],b[3][3],c[3][3],k;
printf(\n enter elements in matrix a);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
scanf(%d,&a[i][j]);
}
printf(enter the elements in matrix b);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
scanf(%d,&b[i][j]);
}
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
c[i][j]=0;
for(k=0;k<3;k++)
c[i][j]=c[i][j]+(a[i][j]*b[i][j]);
}
printf(\n enter the resultant matrix is );
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
printf(%3d,c[i][j]);
}
printf(\n);
}

46. write a program to implement upper triangular and lower


triangular matrix
#include<stdio.h>
main()
{
int a[3][3],i,j;
printf("enter the elements\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
scnaf("%d",&a[i][j]);
}
25
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

printf("lower triangular matrix\n");


for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i<j)
printf(" ");
if(i>=j)
printf("%5d",a[i][j]);
}
printf("\n");
}
printf("upper triangular matrix\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i>j)
printf(" ");
if(i<=j)
printf("%5d",a[i][j]);
}
printf("\n");
}
}

47. write a program to implement symmetric matrix


#include<stdio.h>
main()
{
int i,j,n,a[20][20],b[20][20],flag=0;
printf("enter the order of the matrix A:");
scanf("%d",&n);
printf("enter the elements in matrix A");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
scanf("&d",&a[i][j]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
26
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

b[i][j]=a[i][j];
}
}
printf("transpose of a given matrix is :\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%5d",b[i][j]);
printf("\n");
}
}
for(i=0;i<n;i++)
{
for9j=0;j<n;j++)
if(a[i][j]!=b[i][])
flag=1;
}
if(flag)
printf("\n matrix is not symmetric\n");
else
printf("\n matrix is symmetric");
}

48. write a program to implement Diagonal matrix


#include<stdio.h>
main()
{
int a[3][3],i,j;
printf("enter the elements in to a matrix\n");
for(i=0;i<3;i++)
fro(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
printf("diagonal of matrix\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j)
printf("%3d",a[i][j]);
else
printf(" ");
27
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

}
printf("\n");
}
}

49. write a program to implement combining two strings


#include<stdio.h>
main()
{
int i,j;
char s1[10],s2[10],s3[20];
printf("enter two strings");
scanf("%s%s",s1,s2);
for(i=0;s1[i]!='\0';i++)
for(j=0;s2[j]!='\0';j++)
{
s1[i]=s2[j];
i++;
s1[i]='\0';
for(i=0;s1[i]!='\0';i++)
{
s3[i]=s1[i];
}
s3[i]='\0';
printf("s3 is %s",s3);
}

50. write a program to implement comparison of two strings


#include<stdio.h>
main()
{
int i,f;
char s1[10],s2[10];
printf("enter 1st string");
scanf("%s",&s1);
printf("enter 2nd string");
scanf("%s",&s2);
for(i=0;s1[i]!='\0'&&s2[i]!='\0';i++)
{
if(s1[i]==s2[i])
f=0;
else
f=1;
28
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

}
if(f==0)
printf("strrngs are equal");
else
printf("strings are not equal");
}

51. write a program to implement copying string into another


#include<stdio.h>
main()
{
char string1[30],string[30];
int i;
printf("enter a string");
scanf("%s",string2);
for(i=0;string2[i]!='\0';i++)
string1[i]=string2[i];
string1[i]='\0';
printf("\n");
printf("%s\n",string1);
printf("number of characters=%d\n",i);
}

52. write a program to implement palindrome


#include<stdio.h>
#include<conio.h>
void main()
{
char word[56];
int n=0,i,j,palin;
printf("Enter a word\n\a");
scanf("%s",word);
while(word[n]!='\0')
n++;
i=0;
j=n-1;
palin=1;
while(i<j)
{
if(word[i]!=word[j])
{
palin=0;
break;
}
29
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

i++;
j--;

}
if(palin==1)
printf("%s is a palindrome\n\a",palin);
else
printf("%s is not a palindrome\n\a");

53. write a program to implement reverse string


#include<stdio.h>
main()
{
int i,j;
char str[10];
printf("enter string");
scanf("%s",str);
for(i=0;str[i]!='\0';i++)
printf("%d",i);
for(j=i-1;j>0;j--)
{
printf("%s",str[j]);
}
}

54. write a program to implement sorting of strings


#include<stdio.h>
#include<string.h>
main()
{
char str[5][20],temp[20];
int i=0,,j=0;
printf("enter 5 strings");
for(i=0;i<5;i++)
{
scanf("%s",str[i]);
}
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(strcmp(str[i],str[j])>0)
{
30
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
}
}
printf("after sorting");
for(i=0;i<5;i++)
{
printf("%s\n",str[i]);
}
}

55. write a program to implement string handling functions


#include<stdio.h>
#include<string.h>
main()
{
char s1[20],s2[20],s3[20];
int x,l1,l2,l3;
printf("enter two string constants\n");
scanf("%s%s",s1,s2);
x=strcomp(s1,s2);
if(x!=0)
{
printf("\n\n strings are not equal\n");
strcat(s1,s2);
}
else
printf("strings are equal");
strcpy(s3,s1);
l1=strlen(s1);
l2=strlen(s2);
l3=strlen(s3);
printf("\n s1=%s\t length=%d charactres\n",s1,l1);
printf("\n s2=%s\t length=%d charactres\n",s2,l2);
printf("\n s3=%s\t length=%d charactres\n",s3,l3);
}

56. write a program to explain categories of functions


a) No arguments and No return values*/
#include<stdio.h>
31
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

main()
{
square();
}
suqare()
{
int n,m;
ptintf("enter n");
scanf("%d",&n);
m=n*n;
printf("%d",m);
}
b. arguments with no return values
#include<stdio.h>
main()
{
int n;
printf("enter n");
scanf("%d",&n);
square(n);
}
square(int x)
{
iny y;
y=x*x;
printf("%d",y);
}

c. arguments with return values


#include<stdio.h>
main()
{
int k,n;
printf("enter n");
scanf("%d",&n);
k=square(n);
printf("%d",k);
}
square(int x)
{
32
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

int y;
y=x*x;
return y;
}

57. write a program to implement recursion


#include<stdio.h>
main()
{
int n,k;
printf("enter n");
scanf("%d",&n);
k=fact(n);
printf("%d",k);
}
fact(int n)
{
int f=1;
if(n==1)
return 1;
else
f=n*fact(n-1);
return(f) ;
}

58. write a program to explain Automatic variables


#include<stdio.h>
main()
{
int m=1000;
fuction2();
printf("%d",m);
}
function1()
{
int m=10;
printf("%d\n",m);
}
function2()
{
int m=100;
function1();
33
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

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

59. write a program to explain external variable


#include<stdio.h>
#include<conio.h>
int i;
void inc();
void dec();
void main()
{
printf("%d",i);
inc();
inc();
dec();
dec();
}
void inc()
{
i=i+1;
printf("i=%d",i);
}
void dec()
{
int i=i-1;
printf("%d",i);
}

60. write a program to explain static storage class


#include<stdio.h>
#include<conio.h>
void main()
{
increment();
increment();
increment()
}
void increment()
{
static int i=1;
printf("%d",i);
i=i+1;
}
34
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

61. write a program to explain register storage class


#include<stdio.h>
#include<conio.h>
void main()
{
register int i;
for(i=0;i<10;i++)
printf("%d",i);
}

62. write a program to implement call by value


#include<stdio.h>
main()
{
int a=10,b=20;
printf("the values of a and b before swapping ");
printf("a=%d,b=%d",a,b);
swap(a,b);
printf("the values of and b after swapping");
printf("a=%d,b=%d",a,b);
}
swap(int x,int y)
{
int temp;
temp=x;
x=y;
y=temp;
}

63. write a program to implement call by reference


#include<stdio.h>
main()
{
int a=10,b=20;
printf("the values of a and b before swapping ");
printf("a=%d,b=%d",a,b);
swap(&a,&b);
printf("the values of and b after swapping");
printf("a=%d,b=%d",a,b);
}
swap(int *x,int *y)
{
35
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

int temp;
temp=*x;
*x=*y;
*y=temp;
}

64. write a program find the addition of two matrices using


pointers
#include<conio.h>
#include<stdio.h>
int c[3][3];
int *add(int *x,int *y)
{
int i,j,k;
for(i=0,k=0;i<3;i++,k+=3)
for(j=0;j<3;j++)
c[i][j]=*(x+k+j)+*(y+k+j);
/*printf("\n the matrice is:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",c[i][j]);
}
printf("\n");
} */
return &c[0][0];
}
void main()
{
int a[3][3],b[3][3],i,j,*p;
clrscr();
printf("enter first matrice:\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("enter second matrice:\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
p=add(&a[0][0],&b[0][0]);
36
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

printf("\n the matrice is:\n");


for(i=0;i<=6;i+=3)
{
for(j=0;j<3;j++)
{
printf("%d ",*(p+i+j));
}
printf("\n");
}

65. write a program to implement array of pointers


#include<stdio.h>
main()
{
int *arr[4];
int i=31,j=21,k=19,l=71,p;
arr[0]=&i;
arr[1]=&j;
arr[2]=&k;
arr[3]=&l;
for(p=0;p<=4;p++)
printf("%d"\n",arr[p]);
}
}

66. write a program to implement pointer to an array


#include<stdio.h>
int transpose(int (*b)[3],int r,int c);
int print(int(*y)[3],int r,int c);
main()
{
int a[][3]={1,2,3,4,5,6,7,8,9};
int (*x)[3];
x=a;
transpose(a,3,3);
print(a,3,3);
}
int transpose(int (*b)[3],int r,int c)
{
int i,j,t;
37
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

for(i=0;i<r;i++)
for(j=0;j<c;j++)
t=*(*(b+i)+j);
*(*(b+i)+j)=*(*(b+j)+i);
*(*(b+j)+i)=t;
}
print(int (*y)[3],int r,int c)
{
int i,j;
for(i=0;i<r;i++)
for(j=0;J,c;j++)
{
printf("%d",*(*(y+i)+j);
printf("\n");
}
}

67. write a program to implement pointers and functions


#include<stdio.h>
main()
{
int x;
x=20;
change(&x);
printf("%d\n",x);
}
change(int *p)
{
*p=*p+10;
}

68. write a program to implement pointer expression


#include<stdio.h>
main()
{
int a,b,*p1,*p2,x,y,z;
a=12;
b=4;
p1=&a;
p2=&b;
x=(*p1 * *p2)-6;
y=*p1-*p2+4;
38
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

printf("a=%d,b=%d\n",a,b);
printf("x=%d,y=%d\n",x,y);
*p2=*p2+3;
*p1=*p2-5;
z=(*p2)/(*p1);
printf("a=%d,b=%d\n",a,b);
printf("z=%d",z);
}

69. write a program to implement array elements using ptr


#include<stdio.h>
main()
{
int i,*p,x[5];
printf("enter arrayelements");
for(i=0;i<5;i++)
scanf("%d",&x[i]);
p=x;
for(i=0;i<5;i++)
printf("%d",*(p+i));
}

70. write a program to implement sum of array elements using


ptrs
#include<stdio.h>
main()
{
int x[5]={1,2,3,4,5};
int *p,sum=0;
for(p=&x[0];p<=&x[4];p++)
sum=sum+p;
printf("%d",sum);
}

71. write a program to print no.s in reverse order using pointers


#include<stdio.h>
main()
{
int x[5]={1,2,3,4,5};
int *p;
for(p=&x[4];p>=&x[0];p--)
printf("%d",*p);
39
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

72. write a program to explain pointers in one dimensional array


#include<stdio.h>
main()
{
int *p,sum,i;
static int x[5]={5,9,6,3,7};
i=0;
p=x;
sum=0;
printf("element value address\n\n");
while(i<5)
{
printf("x[%d] %d %u\n",i,*p,p);
sum=sum+*p;
i++;
p++;
}
printf("\n sum=%d\n",sum);
printf("\n &x[0]=%u\n",&x[0]);
printf("\np=%u\n",p);
}

73 . write a program to implement pointers and character strings


#incude<stdio.h>
main()
{
char name;
int length;
char *cptr=name;
name="DELHI";
while(*cptr!='\0')
{
printf("%c is stored at address %\u\n",*cptr,cptr);
cptr++;
}
length=cptr-name;
printf("\n length of the string=%d\n",length);
}

74. write a program to implement pointers to structures


struct invent
40
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

{
char *name[20];
int number;
float price;
};
main()
{
struct invent product[3],*ptr;
printf("INPUT\n\n");
for(*ptr=product;ptr<product+3;ptr++)
scanf("%fs%d%f",ptr->name,&ptr->number,&ptr->price);
printf("\nOUTPUT");
ptr=product;
while(ptr<product+3)
{
printf(%-20s%5d%10.2f\n");
ptr->name;
ptr->number;
ptr->price;
ptr++;
}
}

75. write a program to implement program to illustrate files


#include<stdio.h>
main()
{
FILE *fp1;
char c;
printf("data input\n");
fp1=fopen("input","w");
while((c=getchar()!=EOF))
putc(c,fp1);
fclose(fp1);
printf("\n data output\n");
fp1=fopen("input","r");
while((c=getc(fp1)!=EOF))
printf("%c",c);
}

76.write a program to print sname,course,fee using files


41
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

#include<stdio.h>
main()
{
FILE *fp;
charx[10],c[10];
int fee;
fp=fopen("student","w");
for(i=1;i<=3;i++)
{
printf("enter name course,fee");
scanf("%s%s%d",n,c,&f);
fprintf("fp,"%s%s%d\n",n,c,f);
}
fclose(fp);
}

77. write a program to read data from student file


#include<stdio.h>
main()
{
FILE *fp;
charn[10],c[10];
int fee,i;
fp=fopen("student","r");
for(i=1;i<=3;i++)
{
fscanf(fp,"%s%s%d\n",n,c,fee);
fprintf("%s%s%d\n",n,c,fee);
}
fclose(fp);
}

78.write a program to write text into the file


#include<stdio.h>
main()
{
FILE *fp;
char ch;
printf("enter text");
fp=fopen("first.text","w");
while((ch=getchar())!='\0')
42
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

{
fputc(ch,fp);
}
fclose(fp);
}

79.write a program to write text into file using fputs


#include<stdio.h>
main()
{
char name[15];
FILE *fp;
fp=fopen("first","w");
if(fp==NULL)
{
printf("unable to create afile");
}
else
{
printf("enter text");
scanf("%s",name);
fputs(name,fp);
}
fclose(fp);
}

80. program to read text from file using fgets()


#include<stdio.h>
main()
{
FILE *fp;
char text[20];
fp=fopen("first","r");
if(fp==NULL){
printf("unable to open a file");
}
while(fgets(text,20,fp)!=NULL)
{
printf("%s",line);
}
fclose(fp);
}
43
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

81.write a program to implement fseek function


#include<stdio.h>
#include<conio.h>
struct emp
{
int eno;
char ename[20];
}e;
void main()
{
FILE *fp;
fp=fopen("Emp.txt","w");
printf("Enter Empno:");
scanf("%d",&e.eno);
printf("Enter Empname:");
scanf("%s",e.ename);
fwrite(&e, sizeof(e), 1, fp);
fclose(fp);
printf("Data Stored");
fp=fopen("Exmp.txt","r");
fseek(fp, 0L, SEEK_END);
while(fread(&e,sizeof(e),1,fp)!=0)
{
printf("%d",e.eno);
printf("%s",e.ename);
}
fclose(fp);
getch();
}

82.write a progrm using ftell and fseek functions


#include <stdio.h>
#include <conio.h>
void main()
{
FILE *fp;
char ch;
clrscr();
44
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

fp=fopen("i1.c","r");
fseek(fp,40L,0);
while((ch=fgetc(fp))!=EOF)
{
printf("%c",ch);
delay(30);
}
printf("Total size : %ld\n",ftell(fp));
rewind(fp);
printf("Position : %ld\n",ftell(fp));
while((ch=fgetc(fp))!=EOF)
{
printf("%c",ch);
delay(30);
}

getch();

83.write a program to implement error handling in file operations


#include<stdio.h>
main()
{
char filename[10];
FILE *fp1,*fp2;
int i,number;
fp1=fopen("TEST","w");
for(i=0;i<=100;i+=10)
putw(i,fp1);
fclose(fp1);
printf("\n input filename\n");
open_file:
scanf("%s",filemane);
if(fp2=fopen(filename,"r"))==NULL)
45
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

{
printf("cannot open the file\n");
printf("type file name again\n\n");
goto oepn_file;
}
else
for(i=1;i<=20;i++)
{
number=getw(fp2);
if(feof(fp2))
{
printf("\n ran out of dat\n");
break;
}
else
printf("%d\n",number);
}
flcose(fp2);
}

84.Write a program to explain commandline arguments


#include<stdio.h>
main(argc,agrv)
int argc;
char *argv[];
{
FILE *fp;
int i;
char word[15];
fp=fopen(arv[1],"w");
printf("\n no. of arguments in command lone=%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(arv[1],"r")
for(i=2;i<argc;i++)
{
fscanf(fp,"%s",word);
printf("%s",word);
}
fclose(fp);
46
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

printf("\n\n");
for(i=0;i<agrc;i++)
printf("%s\n",i*5,argv[i]);
}

47
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

85. write a program to implement malloc function


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

void main()
{
int *p,n,*temp;
clrscr();
printf("Enter no of elements to be allocated\n");
scanf("%d",&n);
p=(int*)malloc(n*sizeof(int));
temp=p;
for(i=0;i<n;i++)
{
printf("Element%d Address : %u",temp+i);
}
getch();
}

86.write a program to implement calloc function


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct student
{
int stno;
char name[20];
int marks1;
};
void main()
{
struct student *s1,*temp;
48
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

int n,i;
clrscr();
printf("Enter No of Students : ");
scanf("%d",&n);
s1=(struct student*)calloc(n,sizeof(struct student));
temp=s1;

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

printf("Enter Student%d Number,Name,marks1 : ",i+1);


scanf("%d%s%d",&(temp+i)->stno,(temp+i)->name,&(temp+i)->marks1);

temp=s1;
printf("\t\tNumber\t\tName\t\tMarks1\n");
for(i=0;i<n;i++)
{
printf("Student %d : ",i+1);
printf(" %d ",(temp+i)->stno);
printf(" \t\t %s ",(temp+i)->name);
printf(" \t\t %d \n",(temp+i)->marks1);

}
temp=s1;
for(i=0;i<n;i++)
{
printf("Structure%d Address is %u \n",i+1,temp+i);
}
printf("enter the student num : ");
49
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

scanf("%d",&i);
temp=s1;

printf("Student %d Information : ",i+1);


printf(" %d ",(temp+i)->stno);
printf(" \t\t %s ",(temp+i)->name);
printf(" \t\t %d \n",(temp+i)->marks1);
}

87.write a program to implement stack


include<stdio.h>
#include<stdlib.h>
# define MAX 100
int stack[MAX];
int top=0;
void push();
void pop();
void display();
main()
{
int ch;
while(1)
{
printf("enter your chioce\n");
printf("1.add new item\n");
printf("2.dletee a item from the stack\n");
printf("3.display\n");
printf("4.quit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:exit(0);
}
50
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

}
}
void push()
{
int n;
if(top>=MAX)
printf("stack i already full,can't add more items\n");
else
{
printf("enter the item");
scanf("%d",&n);
stack[top]=n;
top++;
}
}
void pop()
{
if(top<0)
{
printf("stack is already empty");
}
else
top--;
}
void display()
{
int i;
for(i=0;i<top;i++)
{
printf("%d",stack[i]);
}
}

88.write a program to implement stack using pointers


#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100
struct stack
{
int top;
int element[MAXSIZE];
};
int isempty(struct stack *s)
{
51
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

return(s->top==-1);

void pop(struct stack *s)


{
if(!isempty(s))
s->top=s->top-1;
}
int top(struct stack *s)
{
if(!isempty(s))
return(s->element[s->top]);
}
void push(int x,struct stack *s)
{
s->element[++s->top]=x;
}
void display(struct stack *s)
{
int j;
for(j=s->top;j>=0;j--)
printf("-->%3d",s->element[j]);
printf("\n");
}
void menu()
{
printf("1.push\n");
printf("2.pop\n");
printf("3.top element\n");
printf("4.isempty\n");
printf("5.display\n");
printf("6.exit\n");
}
main()
{
struct stack s;
int chioce=1,value;
s.top=-1;
while(1)
{
switch(chioce)
{
case 1:printf("enter the element to be pushed\n");
52
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

scanf("%d",&value);
push(value,&s);
break;
case 2:pop(&s);
break;
case 3:value=top(&s);
printf("the top element of stack i %d\n",value);
break;
case 4:if(!isempty(&s))
printf("stack is not empty\n");
else
printf("stack is empty\n");
break;
case 5:printf("the elements of stack are\n");
display(&s);
break;
case 6:exit(0);
}
menu();
printf("enter u r chioce\n");
scanf("%d",&chioce);
}
}

89.write a program to implement queue


#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 5
void print_q();
void add_q();
int delete_q();
int queue[MAXSIZE];
int front=-1, rear=-1;

main()
{
int choice;
while(1)
{
printf("1.add new item\n");
printf("2.delete a item\n");
53
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

printf("3.dispaly\n");
printf("4.exit\n");
printf("enter u r choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1: add_q();
break;
case 2:delete_q();
break;
case 3:print_q();
break;
case 4:exit(0);
}
}
}
void add_q()
{
int item;
if(rear==MAXSIZE-1)
printf("queue is full,can't add new item\n");
else
{
printf("enter an item\n");
scanf("%d",&item);
if(front==-1)
front=0;
rear++;
queue[rear]=item;
}
}
int delete_q()
{
if(front==rear)
{
printf("queue is empty\n");
front=-1;
rear=-1;
}
else
front++;
}
void print_q()
54
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

{
int i;
if(front==rear)
printf("queue is empty\n");
else
{
for(i=front;i<=rear;i++)
printf("-->%d",queue[i]);
}
printf("\n");
}

90.write a program to implement queue using pointers


#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct queue
{
int no;
struct queue *next;
};
struct queue *start=NULL;
void add();
void del();
void traverse();

void main()
{
int value,ch;
char choice;
clrscr();
do
{

55
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

printf("\n\t\t----1. add\n");
printf("\n\t\t----2. delete\n");
printf("\n\t\t----3. traverse\n");
printf("\n\t\t----4. exit\n");
printf("\n\t\tEnter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: add();
break;
case 2: del();

break;
case 3: traverse();
break;
case 4: return;
default : printf("\nwrong choice\n");
};
}
while(choice!=4);
scanf("%c",&choice);
}
void add()
{
struct queue *p,*temp;
temp=start;
p=(struct queue*)malloc(sizeof(struct queue));
printf("Enter the data : ");
scanf("%d",&p->no);
p->next=NULL;
if(start==NULL)
{
start=p;
}
else
{
while(temp->next!=NULL)
{
temp=temp->next;
56
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

}
temp->next=p;
}
}
void del()
{
struct queue *temp;
int value;
if(start==NULL)
{
printf("\nqueue is empty\n");
getch();
//return(0);
}
else
{
temp=start;
value=temp->no;
start=start->next;
printf("\nthe deleted element is %d ",value);
free(temp);
}
//return(value);
}
void traverse()
{
struct queue *temp;
temp=start;
if(temp!=NULL)
{
while(temp->next!=NULL)
{
printf("\n no= %d",temp->no);
temp=temp->next;
}
printf("\n no= %d ",temp->no);
57
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

}
else
{
printf("QUE is empty"
);
}
getch();
}

91.write a program to implement postfix expression


#include<stdio.h>
#include<ctype.h>
#include<math.h>
float stack[10];
int top=-1;
void push(char);
float pop();
float exp_eval(char[],float[]);
main()
{
int i,j=0;
char postfix[20];
float num[20],result;
printf("\n enter a postfix expression\n");
gets(postfix);
while(postfix[j]!='\0')
{
if(isalpha(postfix[j]))
{
fflush(stdin);
printf("enter a number for%c",postfix[j]);
scanf("%d",&num[j]);
}
j++;
}
result=exp_eval(postfix,num);
printf("the result of %s=%f",postfix,result);
}
float exp_eval(char postfix[],float data[])
58
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

{
int j=0;
float opA,opB,res;
char ch;
while(postfix[j]!='\0')
{
ch=postfix[j];
if(isalpha(postfix[j]))
{
push(data[j]);
}
else
{
opB=pop();
opA=pop();
switch(ch)
{
case '+':push(opA+opB);
break;
case '-':push(opA-opB);
break;
case'*':push(opA*opB);
break;
case'/':push(opA/opB);
break;
case'^':push(pow(opA,opB));
break;
}
}
j++;
}
res=pop();
return(res);
}
void push(char c)
{
top=top+1;
stack[top]=c;
}
float pop()
{
float n;
59
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

n=stack[top];
top=top-1;
return(n);}

92.write a program to evaluate infix to postfix expression


#include<stdio.h>
#include<string.h>
char stack[20];
int top=-1;
void push(char ch)
{
stack[++top]=ch;
}
char pop()
{
return(stack[top--]);
}
int priority(char ch)
{
int r;
switch(ch)
{
case'^':r=5;
break;
case'*':
case'/':r=4;
break;
case'+':
case'-':r=3;
break;
case'(':r=2;
break;
case')':r=1;
break;
default:r=0;
}
return(r);
}
void main()
{
char infix[20],postfix[20];
int i,j=0;
printf("enter infix expression\n");
gets(infix);
60
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

for(i=0;infix[i]!='\0';i++)
{
if(infix[i]=='(')
push(infix[i]);
else if(infix[i]==')')
{
while(stack[top]!='(')
postfix[j++]=pop();
pop();
}
else if(infix[i]>=97&&infix[i]<=122)
postfix[j++]=infix[i];
else
{
if((priority(stack[top])<priority(infix[i])||top==-1))
push(infix[i]);
else
postfix[j++]=pop();
}
}
while(top>=0)
postfix[j++]=pop();
postfix[j]='\0';
printf("postfix expresion is %s",postfix);
}

93.write a program to implement graphs


#include <stdio.h>
#include <conio.h>
int i,j,c[10],graph[10][10],num,span[10][10];
void in();
void out();
void in()
{
61
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

printf("Enter the number of nodes:-> ");


scanf("%d",&num);
printf("GRAPH IS REPRESENTED USING A ADJECENCY MATRIX \n");
for(i=0;i<num;i++)
{
c[i]=i;
graph[i][i]=0;
for(j=i+1;j<num;j++)
{
graph[i][j]=0;
printf("\nEnter the value for edge from node %d to %d -> ",i,j);
scanf("%d",&graph[i][j]);
graph[j][i]=graph[i][j];
}

void out()
{
printf("\nADJECENCY MATRIX FOR THE GRAPH IS \n\n");

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

printf("%c",(char)179);
for(j=0;j<num;j++)
printf(" %d ",graph[i][j]);
printf("%c\n",(char)179);
}
62
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

for(i=0;i<num;i++)
for(j=i;j<num;j++)
{
span[i][j]=0;
if((c[i]!=c[j])&&(graph[i][j]!=0))
{
span[i][j]=graph[i][j];
if(c[i]<c[j])
c[j]=c[i];
else
c[i]=c[j];
}

span[j][i]=span[i][j];

printf("\nADJECENCY MATRIX FOR THE SPANNING TREE IS\n");


for(i=0;i<num;i++)
{
printf("\n%d",(char)179);
for(j=0;j<num;j++)
printf("%d",span[i][j]);
printf("%c",(char)179);
}
}
void main()
{
clrscr();
in();
out();
getch();
}
63
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

94. write a program to implement Linear Search


#include<stdio.h>
#include<conio.h>
void main()
{
int a[32],pos=0,scount=0,key=0,n,i;
clrscr();
printf("Enter the no. of digits\n\a");
scanf("%d",&n);
printf("Enter the elements one by one\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the key element to be searched\n\a");
scanf("%d",&key);
for(i=0;i<n;i++)
{
if(a[i]==key)
{
scount=1;
pos=i;
}
}
if(scount==1)
printf("Element found at %d and the element is %d\n\a",pos,key);
else
printf("Element not found\n\a");

getch();

95. write a program to implement Binary Search


#include<stdio.h>
#include<conio.h>
void main()
64
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

int a[34],first,last,mid,n,key,i,pos=0;
clrscr();
printf("Enter no of digits:\n\a");
scanf("%d",&n);
printf("Enter the elements one by one\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("The key element to be searched\n");
scanf("%d",&key);
printf("\nArray after sorting\n");
for(i=0;i<n;i++)
printf("%4d",a[i]);
first=0;
last=n-1;
while(first<=last)
{
mid=(first+last)/2;
if(a[mid]<key)
last=mid-1;
else if(a[mid]>key)
first=mid+1;
else
{
pos=mid;
break;
}
}
if(a[mid]==key)
printf("\n Element found at location %d\n\a",pos);
else
printf("\nElement not found\n\a");
getch();

96. write a program to implement Bubble sort


65
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,a[30],temp;
clrscr();
printf("Enter how many digits?\n\a");
scanf("%d",&n);
printf("Enter the numbers one by one\n\a");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nThe array before sorting\n\a");
for(i=0;i<n;i++)
printf("%4d",a[i]);
/*bub now*/
for(i=0;i<n-1;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;
}
}
printf("\nThe array after sorting\n\a");
for(i=0;i<n;i++)
printf("%4d",a[i]);
getch();
}

97. write a program to implement Selection sort

#include<stdio.h>
66
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

#include<stdlib.h>
#include<conio.h>
void main()
{
int i,n,a[45],pos;
clrscr();
printf("\nEnter how many elements\n");
scanf("%d",&n);
printf("\nEnter the numbers one by one\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\nThe given array is\n");
for(i=0;i<n;i++)
printf("%4d",a[i]);
for(i=0;i<n-1;i++)
{
pos=min_ele(a,n,i);
exchange(a,i,pos);
}
printf("\nArray after sorting\n");
for(i=0;i<n;i++)
printf("%4d",a[i]);
getch();

}
min_ele(int a[ ],int n,int i)
{
int pos,j;
pos=i;
for(j=i+1;j<n;j++)
if(a[pos]>a[j])
pos=j;
return(pos);
}

exchange(int a[ ],int i,int pos)


{
int temp;
67
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

temp=a[i];
a[i]=a[pos];
a[pos]=temp;
return;
}

98. write a program to implement quick sort


#include<stdlib.h>
#include<stdio.h>
#define MAXSIZE 500
Void quicksort(int elements[],int maxsize);
void sotr(int elements[],int left,int right);
int elements[MAXSIZE];
int main()
{
int i,maxsize;
printf(how amny elements you want to sort:);
scnaf(%d,&maxsize);
printf(\n enter the values one by one:);
for(i=0;i<maxsize;i++)
printf([%i],elements[i]);
printf(\n);
quicksort(elements,maxsize);
printf(\n array after soting\n);
for(i=0;i<maxsize;i++)
printf([%i],elements[i]);
}
void quicksort(int elements[],int maxsize)
{
sort(elements,0,maxsize-1);
}
void quicksort(int elements[],int left,int right)
{
Int pivot,l,r;
L=left;
R=right;
pivot=elements[left];
while(left<right)
{
while((element[right]>=pivot)&&(left<right))
right--;
if(left!=right)
68
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

{
elements[left]=elements[right];
left++;
}
while((elements[left]<=pivot)&&(left<right))
left++;
if(left!=right)
{
elements[right]=elements[left]);
right--;
}
}
elements[left]=pivot;
Pivot=left;
left=l;
right=r;
if(left<pivot)
sort(elements,left,pivot-1);
if(right>pivot)
sort(elements,pivot+1,right);
}

99. write a program to implement insertion sort


#include<stdio.h>
#incliude<stdlib.h>
#define MAXSIZE 500
void insertionsort(int elements[],int maxsize);
int elements[MAXSIZE],maxsize;
int main()
{
int i;
printf(\n how many elements you want to sort:);
scanf(%d,&maxsize);
printf(\n enter the values oe by one);
for(i=0;i<maxsize;i++)
{
printf(\n enter element %i,i);
scanf(%d,&elements[i]);
}
printf(\n array befor e sorting\n);
for(i=0;i<maxsize;i++)
69
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

printf([%i],elemnets[i]);
printf(\n\n);
insertionsort(elements,int maxsize);
printf(\n Array after sorting:\n);
for(i=0;i<maxsize;i++)
printf([%i],elements[i]);
}
Void insertionsort(int elements[],int maxsize)
{
int i,j,index;
for(i=1;i<maxsize;i++)
{
index=elements[i];
j=i;
while(j>0)&&(elemnts[j-1]>index))
{
elements[j]=elements[j-1];
j=j-1;
}
Elements[j]=index;
}
}

100.write a program to implement heap sort


#include<stdio.h>
#include<iostream.h>
void build_heap(int a[],int n)
{
int ele,s,f,i;
for( i=1;i<=n;i++)
{
ele=a[i];
s=i;
f=s/2;
while((f>0)&&a[f]<ele)
{
a[s]=a[f];
s=f;
f=s/2;
}
a[s]=ele;
}

}
void heap_sort(int a[],int n)

70
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

C $ DS MANUAL

SVS GROUP OF INSTITUTIONS

{
int i,temp;
build_heap(a,n);
for(i=n;i>=2;i--)
{
temp=a[i];
a[i]=a[1];
a[1]=temp;
build_heap(a,n);
}

}
int main()
int a[6],i;
printf("enter values\n");
for(i=1;i<=5;i++)
scanf("%d",&a[i]);
heap_sort(a,5);
for(i=1;i<=5;i++)
printf("%d ",a[i]);
return 0;
}

71
SVS GROUP OF INSTITUTIONS
BHEEMARAM,HANAMKONDA

Vous aimerez peut-être aussi