Aim: Write a C Program to find the sum of individual digits of positive

advertisement
Aim: Write a C Program to find the sum of individual digits of positive integer.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,s=0;
clrscr()
printf(“enter any positive integer”);
scanf(“%d”,&n)
while(n > 0)
{
r = n % 10;
s = s + r;
n=n / 10;
}
printf(“the sum of individual digits of positive integer=%d”,s);
getch();
}
Output:
enter any positive integer
234
the sum of individual digits of positive integer=9
Page No:
Aim: Write a c Program to generate the first n terms of the fibonacci sequence.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,a=0,b=1,c;
clrscr();
printf(“enter any number”);
scanf(“%d”,&n);
printf(“ the fibonacci sequence”);
printf(“%d\t%d\t”,a,b);
for(i=3; i<=n;i++)
{
c=a+b;
printf(“%d\t”,c);
a=b;
b=c;
}
getch();
}
Page No:
Output:
enter any number
8
the fibonacci sequence
0
1
1
2
3
5
8
13
Page No:
Aim: Write a c Program to generate all the prime numbers between 1 and n where n is
a value supplied by the user.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n,count;
clrscr();
printf(“enter any number”);
scanf(“%d”,&n);
printf(“prime numbers between 1 and %d\n”,n);
for(i=1; i<=n; i++ )
{
j=1;
count=0;
while(j<=i)
{
if(i%j==0)
count++;
j++;
}
if(count==2)
Page No:
printf(“%d\t”,i);
}
}
Output:
enter any number
20
prime numbers between 1 and 20 :
2
3
5
7
11
13
17
19
Page No:
Aim: Write a c Program to calculate the following
sum = 1 – x2 / 2! + x4 /4! – x6 / 6! + x8 /8! – x10 /10!
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
long int i,j,k=2,f,x;
double s=0;
clrscr();
for(i=2;i<=10;i=i+2)
{
f=1;
for(j=1;j<=i;j++)
f=f*j;
if(k%2==0)
s= s-pow(x,i)/(float)f;
else
s= s+ pow(x,i)/(float)f;
k++;
Page No:
}
printf("the sum=%f",s);
getch();
}
Output:
the sum=-3.293402312390023230000000000000000000000e+78
Page No:
Aim: Write a c Program to find the roots of a quadratic equation.
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b,c;
float d,r1,r2;
clrscr();
printf("enter values of a,b &c");
scanf("%d%d%d",&a,&b,&c);
d=b*b - 4*a*c;
if( d==0)
{
r1=r2=-b/2*a;
printf("roots are equal\n");
printf("root1=root2=%f",r1);
}
else if(d>0)
{
r1=-b+sqrt(d)/(float)2*a;
r2=-b-sqrt(d)/(float)2*a;
Page No:
printf("roots are real\n");
printf("root1=%f\troot2=%f",r1,r2);
}
else
{
printf("roots are imaginary");
}
getch();
}
Output1:
enter values of a,b &c
242
roots are equal
root1=root2=-4.000000
Output2:
enter values of a,b &c
142
roots are real
root1=-2.585786
root2=-5.414214
Output3:
enter values of a,b &c
145
roots are imaginary
Page No:
Aim : Write a c Program to find the factorial of a given integer using non recursive
function
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int n,f;
int fact(int);
clrscr();
printf(“enter any integer”);
scanf(“%d”,&n);
f=fact(n);
printf(“the factorial of a given number=%d”,f);
getch();
}
int fact(int n)
{
int f=1,i;
for(i=1;i<=n;i++)
f=f*i;
}
return(f)
Page No:
}
Output:
enter any integer
5
the factorial of a given number =120
Page No:
Aim : Write a c Program to find the factorial of a given integer using recursive function
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int n,f;
int fact(int);
clrscr();
printf(“enter any integer”);
scanf(“%d”,&n);
f=fact(n);
printf(“the factorial of a given number=%d”,f);
getch();
}
int fact(int n)
{
int f;
if(n==1)
return 1;
else
f=n*fact(n-1);
return (f);
Page No:
}
Output:
enter any integer
5
the factorial of a given number =120
Page No:
Aim: Write a c Program to find the lcm and gcd of two given integers using nonrecursive function
Program :
#include<stdio.h>
#include<conio.h>
void lcm(int,int);
int gcd(int,int);
void main()
{
int a,b,z;
clrscr();
printf("\n enter a,b values");
scanf("%d%d",&a,&b);
if(a<b)
{
z=gcd(a,b);
printf("\n the gcd of%d and %d is %d",a,b,z);
}
else
{
z=gcd(b,a);
printf("\n the gcd of%d and %d is %d",b,a,z);
}
lcm(a,b);
getch();
}
int gcd(int c,int d)
{
int r;
r=d%c;
while(r!=0)
{
d=c;
c=r;
r=d%c;
}
return(c);
}
void lcm(int x,int y)
Page No:
{
int l;
if(x<y)
l=((x*y)/gcd(x,y));
else
l=((x*y)/gcd(x,y));
printf("\n l.c.m of %d and %d is %d",x,y,l);
}
Output::
enter a,b values6 5
the gcd of5 and 6 is 1
l.c.m of 6 and 5 is 30
Page No:
Aim: the total distance traveled by a vehicle in t secs is given by distance=ut+1/2at*t
where u and a are initial velocity and a is acceleration. Write a c Program to find the
distance traveled atregular intervals of the time for the given values of u and a. the
Program should provideflexibility to user to select his own time intervals and repeat
calculations for different u and a.
Program
#include<stdio.h>
#include<conio.h>
void main()
{
float s,u,a,sum=0;
int t,k,d;
clrscr();
printf("\nenter velocity and acceleration of vehicle");
scanf("%f %f",&u,&a);
printf("\nenter final time");
scanf("%d",&k);
printf("\nenter time interval");
scanf("%d",&d);
for(t=0;t<=k;t=t+d)
{
s=(u*t)+(0.5*a*t*t);
printf("\ndistance travelled is %f",s);
sum=sum+s;
}
printf("\nsum of distance is %f",sum);
getch();
}
Output::
enter velocity and acceleration of vehicle50 10
enter final time2
enter time interval1
distance travelled is 0.000000
Page No:
distance travelled is 55.000000
distance travelled is 120.000000
sum of distance is 175.000000
Aim:Program to read two numbers and perform desired arithmetic operation.
Program:
Page No:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,d;
clrscr();
printf("enter the values a and b");
scanf("%d%d",&a,&b);
printf("\n 1.addition");
printf("\n 2.subtraction");
printf("\n 3.multiplication");
printf("\n 4.division");
printf("\n 5.modulo division");
printf("\n enter your choice from 1-5:");
scanf("%d",&d);
switch(d)
{
case 1:
printf("\n addition");
c=a+b;
printf("\n the result is =%d",c);
break;
case 2:
printf("\n subtraction");
c=a-b;
printf("\n the result is=%d",c);
break;
case 3:
printf("\n multiplication");
c=a*b;
printf("\n the result is=%d",c);
break;
case 4:
printf("\n division");
c=a/b;
printf("\n the result is=%d",c);
break;
case 5:
printf("\n modluo division");
Page No:
c=a%b;
printf("\n the result is=%d",c);
break;
default:
printf("\n wrong entry");
break;
}
getch();
}
Output::
enter the values a and b 5 6
1.addition
2.subtraction
3.multiplication
4.division
5.modulo division
enter your choice from 1-5:3
multiplication
the result is=30
Aim: Write a c Program to find both the largest and smallest number in a list of
integers
Page No:
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,a[10],largest,smallest;
clrscr();
printf("how many numbers you want to enter:");
scanf("%d",&n);
printf("enter %d elements",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
largest=a[0];
smallest=a[0];
for(i=1;i<n;i++)
{
if(largest<a[i])
largest=a[i];
if(smallest>a[i])
smallest=a[i];
}
printf("\n largest number =%d\n",largest);
Page No:
printf("smallest number=%d",smallest);
getch();
}
Output:
how many numbers you want to enter:5
enter 5 elements
10
20
30
40
50
largest number =50
smallest number=10
Aim: Write a c Program to find addition of two matrices
Program:
Page No:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[2][2],b[2][2],c[2][2],r1,c1,r2,c2,i,j;
clrscr();
printf("enter no of rows & columns of matrix-a");
scanf("%d%d",&r1,&c1);
printf("enter no of rows & columns of matrix-b");
scanf("%d%d",&r2,&c2);
if(r1==r2 && c1==c2)
{
printf("enter elements of matrix-a");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter elements of matrix-b");
for(i=0;i<r2;i++)
Page No:
{
for(j=0;j<c2;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("matrix addition:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
c[i][j]=a[i][j]+b[i][j];
printf("%2d",c[i][j]);
}
printf("\n");
}
}
else
printf("matrix addition is not possible");
getch();
}
Output:
enter no of rows & columns of matrix-a
Page No:
2
2
enter no of rows & columns of matrix-b
2
2
enter elements of matrix-a
1
2
3
4
enter elements of matrix-b
5
6
7
8
matrix addition:
6
8
10
12
Aim: Write a c Program to find multiplication of two matrices
Program:
Page No:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[2][2],b[2][2],c[2][2],i,j,m,n,p,q,k;
clrscr();
printf("\n enter the order of matrix-a");
scanf("%d%d",&m,&n);
printf("\n enter order of matrix b");
scanf("%d%d",&p,&q);
if(n==p)
{
printf("enter elements of matrix-a");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
printf("enter elements of matrix b");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
Page No:
scanf("%d",&b[i][j]);
}
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<p;k++)
{
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
}
}
printf("\n multiplication of matrix is\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%2d",c[i][j]);
}
printf("\n");
}
}
Page No:
else
printf("\n matrix multiplication is not possible");
getch();
}
Output:
enter the order of matrix-a
2
2
enter order of matrix b
2
2
enter elements of matrix-a
1
2
3
4
enter elements of matrix b
1
1
1
1
multiplication of matrix is
3
3
7
7
Output2:
enter the order of matrix-a
Page No:
2
2
enter order of matrix b
1
2
matrix multiplication is not possible
Aim: Write a c Program to delete n characters from a given position in given string
Program:
Page No:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,pos,n;
char mstr[100];
void strdel();
clrscr();
printf("enter main string");
gets(mstr);
printf("\n enter how many characters and from which position");
scanf("%d%d",&n,&pos);
strdel(mstr,n,pos);
getch();
}
void strdel(char m[],int n,int p)
{
int i;
for(i=p; m[i]!='\0';i++)
m[i]=m[i+n];
Page No:
printf("after deletion=%s",m);
}
Output:
enter main string : hyderabad
enter how many characters and from which position
3
3
after deletion=hydbad
Aim: Write a c Program to determine if the given string is a palindrome or not.
Program:
Page No:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j=0,len=0;
char s1[10],s2[10];
clrscr();
printf("enter any string:");
scanf("%s",s1);
for(i=0;s1[i]!='\0';i++)
{
len++;
}
for(i=len-1;i>=0;i--)
{
s2[j]=s1[i];
j++;
}
s2[j]=null;
for(i=0;s1[i]!='\0';i++)
{
Page No:
if(s1[i]==s2[i])
{
continue;
}
else
{
printf("not palindrome");
getch();
exit(0);
}
}
printf("palindrome");
getch();
}
Output:
enter any string:liril
palindrome
Output2:
enter any string:india
not palindrome
Aim: Write a C program that displays the position or index in the string S where the
string T begins or -1 if S does’t not contain T
Page No:
Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s[20],t[10];
char *found;
clrscr();
printf("enter the main string");
gets(s);
printf("enter the string to be searched");
gets(t);
found=strstr(s,t);
if(found)
printf("string t is found in the main string at %d position",found+1);
else
printf("-1");
getch();
}
Output:
enter the main string :
Page No:
hyderabad
enter the string to be searched:
bad
string t is found in the main string at 7 position
Aim: Write a c Program to count the lines words and characters in a given text.
Program:
Page No:
#include<stdio.h>
#include<conio.h>
void main()
{
int line=0,word=0,ch=0;
char c;
clrscr();
printf("enter text");
while((c=getchar())!=eof)
{
if (c=='\n')
line++;
else if(c==' ')
word++;
else
ch++;
}
printf("no of lines=%d",line);
printf("\n no of words=%d",word+line);
printf("\n no of characters=%d",ch);
getch();
}
Output:
Page No:
enter textindia is my country
all indians are my brothers and sisters
^z
no of lines=2
no of words=11
no of characters=49
Aim: Write a c Program to generate pascal’s triangle.
Program
Page No:
#include<stdio.h>
#include<conio.h>
void main()
{
int b,p,q,r,x;
clrscr();
printf("\n enter no.of rows");
scanf("%d", &r);
b=1;
q=0;
printf("\n pascal's triangle\n");
while(q<r)
{
for(p=30-3*q;p>0;p--)
printf(" ");
for(x=0;x<=q;x++)
{
if(x==0||q==0)
b=1;
else
b=(b*(q-x+1)/x);
printf("%6d",b);
}
printf("\n");
q++;
}
getch();
}
Output::
enter no.of rows 5
Page No:
pascal's triangle
1
1
1
1
1
1
2
3
4
1
3
6
1
4
1
Aim: Write a c Program to construct a pyramid of numbers
Page No:
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,k,x=1;
clrscr();
printf("enter no of rows");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(k=n-i;k>=1;k--)
printf(" ");
for(j=1;j<=i;j++)
{
printf("%4d",x);
x++;
}
printf("\n");
}
getch();
}
Page No:
Output:
enter no of rows 4
1
2 3
4 5 6
7 8 9 10
Page No:
Aim: Write a c Program to read in two numbers x and n and the compute the sum of
this geometric progression.
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int x,n,i,s=1;
clrscr();
label:
printf("enter values of x and n");
scanf("%d%d",&x,&n);
if(n>0)
{
for(i=1;i<=n;i++)
{
s=s+pow(x,i);
}
printf("sum=%d",s);
}
else
{
Page No:
printf("enter positive integer for n\n");
goto label;
}
getch();
}
Output:
enter values of x and n
5
3
sum=156
Output2:
enter values of x and n
5
-2
enter positive integer for n
enter values of x and n
Page No:
Aim: Program to perform addition,subtraction,multiplication and division on two
complex numbers.((x+iy) & (a+ib)).
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,a,b;
void add(int,int,int,int);
void sub(int,int,int,int);
void mul(int,int,int,int);
void div(float,float,float,float);
clrscr();
printf("\n enter real and imaginary part of 1st and 2nd numbers");
scanf("%d%d%d%d",&a,&b,&x,&y);
printf("\n numbers are:%d+i%d,%d+i%d",x,y,a,b);
add(x,y,a,b);
sub(x,y,a,b);
mul(x,y,a,b);
div(x,y,a,b);
getch();
}
void add(int p,int q,int r,int s)
{
int real,imag;
real=p+r;
imag=q+s;
printf("\n sum=%d+i%d",real,imag);
}
void sub(int p,int q,int r,int s)
{
int real,imag;
real=p-r;
imag=q-s;
printf("\n diffrence=%d+i%d",real,imag);
}
void mul(int p,int q,int r,int s)
{
Page No:
int real,imag;
real=p*r-q*s;
imag=q*r+p*s;
printf("\n product=%d+i%d",real,imag);
}
void div(float p,float q,float r,float s)
{
float real,imag;
real=(p*r+q*s)/(r*r+s*s);
imag=(q*r-p*s)/(r*r+s*s);
printf("\n division=%f+i%f",real,imag);
}
Output:
enter real and imaginary part of 1st and 2nd numbers 7 8 4 6
numbers are:4+i6,7+i8
sum=11+i14
diffrence=-3+i-2
product=-20+i74
division=0.672566+i0.088496
Page No:
Aim: Write a c Program which copies one file to another.
Program.
#include<stdio.h>
#include<conio.h>
void main()
{
file *f1,*f2;
char c;
clrscr();
f1=fopen("sample","w");
printf("enter text\n");
while((c=getchar())!=eof)
{
putc(c,f1);
}
fclose(f1);
f1=fopen("sample","r");
f2=fopen("sample2","w");
while((c=getc(f1))!=eof)
{
putc(c,f2);
}
fclose(f1);
Page No:
fclose(f2);
getch();
}
Output:
enter text
india is my country
^z
f:\turboc3>type sample
india is my country
f:\turboc3>type sample2
india is my country
Page No:
Aim: Write a c Programme to display the contents of a file.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
file *fp;
char c;
clrscr();
fp=fopen("f:\turboc3\sample","r");
printf("contents of a file:\n");
while((c=getc(fp))!=eof)
{
putc(c,stdout);
}
fclose(fp);
getch();
}
Output:
contents of a file:
india is my country
Page No:
Aim: Write a Program to find no of spaces,tabs,lines and characters in a file:
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int line,space,ch,tab;
char c;
file *fp;
fp=fopen("example","w");
printf("enter text\n");
while((c=getchar())!=eof)
{
putc(c,fp);
}
fclose(fp);
fp=fopen("example","r");
line=space=tab=ch=0;
while((c=getc(fp))!=eof)
{
if(c==' ')
space++;
Page No:
else if(c=='\t')
tab++;
else if(c=='\n')
line++;
else
ch++;
}
printf("\nno of spaces=%d",space);
printf("\nno of tabs=%d",tab);
printf("\nno of lines=%d",line);
printf("\nno of characters=%d",ch);
fclose(fp);
getch();
}
Output:
enter text
joseph hyderabad
yosepu secbad
james hydbad
david secbad
^z
no of spaces=2
Page No:
no of tabs=2
no of lines=4
no of characters=49
Page No:
Aim : Write a c Programme to merge two files into a third file.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
file *f1,*f2;
char c;
clrscr();
f1=fopen("sample","w");
printf("enter text into sample file\n");
while((c=getchar())!=eof)
{
putc(c,f1);
}
fclose(f1);
f2=fopen("sample","w");
printf("enter text into sample2 file\n");
while((c=getchar()!=eof)
{
putc(c,f2);
}
fclose(f2);
Page No:
f1=fopen("sample","r");
f2=fopen("sample2","r");
f3=fopen("sample3","w");
while((c=getc(f1))!=eof)
putc(c,f3);
while((c=getc(f2))!=eof)
putc(c,f3);
fclose(f1);
fclose(f2);
fclose(f3);
getch();
}
getch();
}
Output:
enter text into sample file
india is my country
^z
enter text into sample2 file
all indians are my brothers and sisters
^z
Page No:
f:\turboc3>type sample
india is my country
f:\turboc3>type sample2
all indians are my brothers and sisters
f:\turboc3>type sample3
india is my country
all indians are my brothers and sisters
f:\turboc3>
Page No:
Aim: Write a c Program to search a key value in a given list of integers using linear
search
Program:
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
int i,n,element,a[10];
clrscr();
printf("enter how many elements u want to enter");
scanf("%d",&n);
printf("enter %d elements",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("enter any element to search");
scanf("%d",&element);
for(i=0;i<n;i++)
{
if(a[i]==element)
{
printf("element is found at position %d",i);
getch();
Page No:
exit(0);
}
}
printf("element is not found");
getch();
}
Output:
enter how many elements u want to enter
5
enter 5 elements
20
30
10
40
50
enter any element to search
30
element is found at 1 position
Page No:
Aim: Aim: Write a c Program to search a key value in a given list of integers using
binary search
Program:
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
int i,n,element,a[10],low,high,mid;
clrscr();
printf("enter how many elements u want to enter");
scanf("%d",&n);
printf("enter %d elements",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("enter any element to search");
scanf("%d",&element);
low=0;
high=n-1;
while(low<=high)
Page No:
{
mid = (low+high)/2;
if(element< a[mid])
high=mid-1;
else if (element > a[mid])
low=mid+1;
else
{
printf("element is found at position =%d",mid);
getch();
exit(0);
}
}
printf("element is not found");
getch();
}
Output:
enter how many elements u want to enter
5
enter 5 elements
10
20
30
Page No:
40
50
enter any element to search
30
element is found at 1 position
Page No:
Aim: Write a c Program to sort a given list of integers in ascending order using bubble
sort.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n,a[10],temp;
clrscr();
printf("enter how many elements u want to enter");
scanf("%d",&n);
printf("enter %d elements",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
for(j=i;j<n-1;j++)
{
if(a[i]>a[j+1])
{
temp=a[i];
a[i]=a[j+1];
a[j+1]=temp;
Page No:
}
}
}
printf("sorted elements in ascending order\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
}
Output:
enter how many elements u want to enter
5
enter 5 elements
20
30
10
50
40
sorted elements in ascending order
10
20
30
40
50
Page No:
Aim: Write a c Program to sort a given list of integers in ascending order using bubble
sort.
Program :
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
int i,j,n,a[10],temp,min;
clrscr();
printf("enter how many elements u want to enter");
scanf("%d",&n);
printf("enter %d elements",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
{
min=i;
for( j=i+1;j<n;j++)
{
if(a[j]<a[min])
Page No:
min=j;
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
printf("after sorting :\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
}
ouput:
enter how many elements u want to enter
5
enter 5 elements
20
30
10
50
40
after sorting
10
Page No:
20
30
40
50
Page No:
Aim: Write a c Program to sort a given list of integers in ascending order using
insertion sort
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n,a[10],index;
clrscr();
printf("enter how many elements u want to enter");
scanf("%d",&n);
printf("enter %d elements",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=1;i<n;i++)
{
index=a[i];
j=i;
while(j>0 && (a[j-1] >index))
{
Page No:
a[j]=a[j-1];
j=j-1;
}
a[j]=index;
}
printf("sorted elements:\n");
for( i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}
ouput:
enter how many elements u want to enter
5
enter 5 elements
20
30
10
50
40
sorted elements:
10
20
30
40
50
Page No:
Aim: Write a c Program to implement merge sort.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,n1,n2,a[10],b[10],c[20];
clrscr();
printf("enter how many elements u want to enter for first array");
scanf("%d",&n1);
printf("enter %d elements of first array",n1);
for(i=0;i<n1;i++)
scanf("%d",&a[i]);
printf("enter how many elements u want to enter for second array");
scanf("%d",&n2);
printf("enter %d elements of second array",n2);
for(i=0;i<n2;i++)
scanf("%d",&b[i]);
i=j=k=0;
while( i<n1 && j<n2)
{
Page No:
if(a[i]<b[j])
{
c[k]=a[i];
i++;
}
else
{
c[k]=a[j];
j++;
}
k++;
}
while(i<n1)
{
c[k]=a[i];
i++;
k++;
}
while(j<n2)
{
c[k]=a[j];
Page No:
j++;
k++;
}
printf("sorted elements:\n");
for( i=0;i<k;i++)
printf("%d\t",c[i]);
getch();
}
Output:
enter how many elements u want to enter for first array
2
enter 2 elements of first array
15
25
enter how many elements u want to enter for second array
3
enter 3 elements of second array
10
20
30
sorted elements:
10
15
20
25
30
Page No:
Aim: Write a c Program that implement stack operations using arrays.
Program:
#include<stdio.h>
#include<conio.h>
#include<process.h>
const int size=3;
int stk[size], top,i;
void push( );
void pop( );
void display( );
void main( )
{
int i,ch;
clrscr( );
while(1)
{
printf("\n1.push \n");
printf("2.pop \n");
printf("3.display \n");
printf("4.exit \n");
printf("enter your choice:");
scanf(“%d”,&ch);
switch(ch)
Page No:
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
printf("the elements are:");
display();
break;
case 4:
exit(0);
default:
printf("enter correct choice:");
break;
}
}
}
void push( )
{
int element;
if(top==size)
Page No:
{
printf("stack is full");
}
else
{
printf("enter any integer:");
scanf(“%d”,&element);
stk[top]=element;
top++;
}
}
void pop()
{
if(top==0)
{
printf("stack is empty");
}
else
{
--top;
printf("the deleted element is:",stk[top]);
Page No:
}
}
void display()
{
for(i=top-1;i>=0;i--)
{
printf(“%d\n”.stk[i]);
}
}
}
Output:
1.push
2.pop
3.display
4.exit
enter your choice : 1
enter any integer: 10
1.push
2.pop
3.display
4.exit
enter your choice : 1
enter any integer: 20
1.push
2.pop
3.display
Page No:
4.exit
enter your choice : 1
enter any integer: 30
1.push
2.pop
3.display
4.exit
enter your choice : 1
stack is full
1.push
2.pop
3.display
4.exit
enter your choice : 3
the elements are 30 20 10
1.push
2.pop
3.display
4.exit
enter your choice : 2
the deleted element is 30
1.push
2.pop
3.display
Page No:
4.exit
enter your choice : 2
the deleted element is 20
1.push
2.pop
3.display
4.exit
enter your choice : 2
the deleted element is 10
1.push
2.pop
3.display
4.exit
enter your choice : 2
stack is empty
1.push
2.pop
3.display
4.exit
enter your choice : 4
Page No:
Aim: Write a c Program to implement queues using arrays
Program:
#include<stdio.h>
#include<conio.h>
#include<process.h>
const int size=10;
int q[size],front,rear,i;
void insert( ) ;
void del( );
void display( );
void main()
{
int i,ch;
clrscr();
while(1)
{
printf("\n1.insert \n");
printf("2.delete \n");
printf("3.display \n");
printf("4.exit \n");
printf("enter your choice:");
scanf(“%d”,&ch);
Page No:
switch(ch)
{
case 1:
insert( );
break;
case 2:
del( );
break;
case 3:
printf("the elements are:");
display( );
break;
case 4:
exit(0);
default:
printf("enter correct choice:");
break;
}
}
}
void insert()
{
int element;
Page No:
if(rear = = size)
{
printf("queue is full");
}
else
{
printf("enter any integer:");
scanf(“%d”,&element);
q[rear]=element;
rear++;
}
}
void del()
{
if((rear==front) || rear==0)
{
printf("queue is empty");
rear=front=0;
}
else
{
printf("deleted element is :",q[front]);
Page No:
front++;
}
}
void display()
{
for(i=front;i<rear;i++)
{
printf(“%d\n”,q[i]);
}
}
}
Output:
1.insert
2.delete
3.display
4.exit
enter your choice: 1
enter any integer: 10
1.insert
2.delete
3.display
4.exit
enter your choice: 1
enter any integer: 20
Page No:
1.insert
2.delete
3.display
4.exit
enter your choice: 1
enter any integer: 30
1.insert
2.delete
3.display
4.exit
enter your choice: 1
queue is full
1.insert
2.delete
3.display
4.exit
enter your choice: 3
the elements are 10 20 30
1.insert
2.delete
3.display
4.exit
enter your choice: 2
deleted element is: 10
Page No:
1.insert
2.delete
3.display
4.exit
enter your choice: 2
deleted element is: 20
1.insert
2.delete
3.display
4.exit
enter your choice: 2
deleted element is: 30
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
Aim: Program For Conversion Of Infix To Postfix
Page No:
Program:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<process.h>
#define SIZE 20
int top= -1;
char stack[SIZE];
char infix[20],postfix[20],op;
void push_pop(int *);
void push(char);
void pop(int *);
int priority(char);
void main()
{
int p=-1,i;
stack[++top]='$';
clrscr();
printf("\n Enter an Infix Expression:");
gets(infix);
i=0;
while (infix[i]!='\0')
{
op=infix[i];
if((op>=48&&op<=57)||(op>=65&&op<=90)||(op>=97&&op<=122))
postfix[++p]=op;
else
push_pop(&p);
i++;
}
op='\0';
while(top!=0)
pop(&p);
printf("\n The Postfix Expression is %s",postfix);
getch();
}
Page No:
void push_pop(int *index)
{
if(op==')')
pop(index);
else
while(priority(op)<=priority(stack[top]))
{
if(stack[top]=='(')
{
push(op);
return;
}
else
pop(index);
}
if(op!=')')
push(op);
}
int priority(char x)
{
int retval;
switch(x)
{
case ')': retval=0;
break;
case '$': retval=1;
break;
case '+':
case '-': retval=2;
break;
case '*': retval=3;
break;
case '/': retval=4;
break;
case '(': retval=5;
break;
case '^': retval=6;
Page No:
break;
case '%': retval=7;
break;
default: printf("\n Invalid Operator found in Infix Expression");
getch();
exit(0);
}
return(retval);
}
void pop(int *index)
{
if(op==')')
{
while(stack[top]!='(')
{
postfix[++(*index)]=stack[top--];
if(top==0)
{
printf("\n Invalid Expression,Left paranthesis missed");
getch();
exit(0);
}
}
top--;
}
else if(stack[top]=='(')
{
printf("\n Invalid Expression,Right paranthesis missed");
getch();
exit(0);
}
else
postfix[++(*index)]=stack[top--];
}
void push(char op)
{
if(top<SIZE-1)
stack[++top]=op;
else
{
Page No:
printf("\nThe Stack reaches Overflow");
getch();
}
}
Output:
Enter an Infix Expression:a*b+c/d*f-g
The Postfix Expression is ab*cd/f*+gEnter an Infix Expression:a+(b*c+d)-f/g
The Postfix Expression is abc*d++fg/-
Aim: Program For Implementing Linear Linked List
Page No:
Program:
#include<conio.h>
#include<stdio.h>
#define null 0
typedef struct node
{
int data;
struct node *next;
}node;
void main()
{
node* delete(node* *list, int x);
void count(node* list);
void search(node* list,int x);
void display(node* list);
void insert(node* *list, int x);
int menu();
int x,ch;
char op;
node *list;
list=null;
clrscr();
Page No:
printf("**********INPUT**********");
do
{
ch=menu();
switch(ch)
{
case 1: printf("\nENTER THE ELEMENT TO BE INSERTED:");
scanf("%d",&x);
insert(&list,x);
break;
case 2: printf("ENTER THE ELEMENT TO BE DELETED:");
scanf("%d",&x);
delete(&list,x);
break;
case 3: display(list);
break;
case 4: count(list);
break;
case 5: printf("\n ENTER THE ELEMENT TO BE SEARCHED:");
scanf("%d",&x);
search(list,x);
break;
case 6: exit(0);
Page No:
break;
default:printf("\n INVALID CHOICE");
getch();
}
printf("\n DO YOU WANT TO CONTINUE(Y/N)?:");
op=getche();
}
while(op=='y'||op=='Y');
}
int menu()
{
int ch;
printf("\n**********MENU**********");
printf("\n1.INSERT");
printf("\n2.DELETE");
printf("\n3.DISPLAY");
printf("\n4.COUNT");
printf("\n5.SEARCH");
printf("\n6.EXIT");
printf("\nENTER YOUR CHOICE:");
scanf("%d",&ch);
Page No:
return(ch);
}
void insert(node* *list,int x)
{
node *p,*q;
p=(node*)malloc(sizeof(node));
p->data=x;
p->next=null;
if(*list==null)
*list=p;
else
{
for(q=*list;q->next!=null;q=q->next)
{ };
q->next=p;
}
}
node* delete(node* *list, int x)
Page No:
{
node *p,*q;
int m=0;
q=*list;
for(p=*list;p!=null;p=p->next)
{
if(p->data==x)
{
if(p==q)
{
q=q->next;
free(p);
*list=q;
m=1;
}
else
{
while(q->next!=p)
q=q->next;
q->next=p->next;
free(p);
m=1;
}
Page No:
}
}
if(m)
printf("\n %d IS DELETED FROM THE LIST",x);
else
printf("\n %d IS NOT PRESENT IN THE LIST",x);
}
void display(node* list)
{
int x;
node*p;
printf("\n THE LIST CONTAINS FOLLOWING ELEMENTS");
for(p=list;p!=null;p=p->next)
{
x=p->data;
printf("\n %d",x);
}
printf("\n");
}
void count(node* list)
{
Page No:
node *p;
int c=0;
p=list;
while(p!=null)
{
++c;
p=p->next;
}
printf("\n NUMBER OF ELEMENTS IN THE LIST ARE %d",c);
}
void search(node* list, int x)
{
node *p;
int m=0;
for(p=list;p!=null;p=p->next)
{
if(p->data==x)
m=1;
}
if(m)
printf("\N THE ELEMENT IS PRESENT");
else
Page No:
printf("\n THE ELEMENT IS NOT PRESENT");
}
OUTPUT
**********INPUT**********
**********MENU**********
1.INSERT
2.DELETE
3.DISPLAY
4.COUNT
5.SEARCH
6.EXIT
ENTER YOUR CHOICE:1
ENTER THE ELEMENT TO BE INSERTED:1
DO YOU WANT TO CONTINUE(Y/N)?:Y
**********MENU**********
1.INSERT
2.DELETE
3.DISPLAY
4.COUNT
5.SEARCH
6.EXIT
Page No:
ENTER YOUR CHOICE:1
ENTER THE ELEMENT TO BE INSERTED:2
DO YOU WANT TO CONTINUE(Y/N)?:Y
**********MENU**********
1.INSERT
2.DELETE
3.DISPLAY
4.COUNT
5.SEARCH
6.EXIT
ENTER YOUR CHOICE:3
THE LIST CONTAINS FOLLOWING ELEMENTS
1
2
DO YOU WANT TO CONTINUE(Y/N)?:Y
**********MENU**********
1.INSERT
2.DELETE
3.DISPLAY
Page No:
4.COUNT
5.SEARCH
6.EXIT
ENTER YOUR CHOICE:4
NUMBER OF ELEMENTS IN THE LIST ARE 2
DO YOU WANT TO CONTINUE(Y/N)?:Y
**********MENU**********
1.INSERT
2.DELETE
3.DISPLAY
4.COUNT
5.SEARCH
6.EXIT
ENTER YOUR CHOICE:5
ENTER THE ELEMENT TO BE SEARCHED:1
THE ELEMENT IS PRESENT
DO YOU WANT TO CONTINUE(Y/N)?:Y
**********MENU**********
1.INSERT
2.DELETE
3.DISPLAY
Page No:
4.COUNT
5.SEARCH
6.EXIT
ENTER YOUR CHOICE:2
ENTER THE ELEMENT TO BE DELETED:1
1 IS DELETED FROM THE LIST
DO YOU WANT TO CONTINUE(Y/N)?:Y
**********MENU**********
1.INSERT
2.DELETE
3.DISPLAY
4.COUNT
5.SEARCH
6.EXIT
ENTER YOUR CHOICE:3
THE LIST CONTAINS FOLLOWING ELEMENTS
2
DO YOU WANT TO CONTINUE(Y/N)?: Y
**********MENU**********
1.INSERT
Page No:
2.DELETE
3.DISPLAY
4.COUNT
5.SEARCH
6.EXIT
ENTER YOUR CHOICE:7
INVALID CHOICE
DO YOU WANT TO CONTINUE(Y/N)?:Y
**********MENU**********
1.INSERT
2.DELETE
3.DISPLAY
4.COUNT
5.SEARCH
6.EXIT
ENTER YOUR CHOICE:6
Aim: Program For Implementaion Of Stack Using Linked List
Page No:
Program:
#include<stdio.h>
#include<alloc.h>
#include<conio.h>
struct linkedstack
{
int num;
struct linkedstack *next;
};
typedef struct linkedstack stack;
stack *push(int,stack*);
stack *pop(stack*);
void printstack(stack*);
void main()
{
int choice,x;
stack *top;
top=NULL;
while(1)
{
clrscr();
printf("\n\t\tMENU");
printf("\n\t1.PUSH");
Page No:
printf("\n\t2.POP");
printf("\n\t3.PRINTSTACK");
printf("\n\t4.EXIT");
printf("\nENTER YOUR CHOICE");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("\n ENTER THE NUMBER");
scanf("%d",&x);
top=push(x,top);
break;
case 2: top=pop(top);
break;
case 3: printstack(top);
break;
case 4: exit(0);
break;
default:printf("\n INVALID CHOICE SELECTED");
getch();
}
}
}
stack *push(int x,stack *top)
Page No:
{
stack *newnode;
if((newnode=(stack*)malloc(sizeof(stack)))==NULL)
{
printf("\n THERE IS NO MEMORY");
getch();
return top;
}
newnode->num=x;
newnode->next=top;
top=newnode;
printf("\n THE %d IS PUSHED ONTO STACK", newnode->num);
getch();
return top;
}
stack *pop(stack *top)
{
stack *temp;
temp=top;
if(temp!=NULL)
{
top=temp->next;
printf("\n THE %d IS POPPED OFF THE STACH", temp->num);
Page No:
free(temp);
}
else
printf("\n THE STACK IS EMPTY,YOU CANNOT POP");
getch();
return top;
}
void printstack(stack *top)
{
stack *temp;
if(top==NULL)
printf("\n THERE ARE NO ELEMENTS IN STACK");
else
{
printf("\n THE STACK ELEMENTS ARE");
temp=top;
while(temp!=NULL)
{
printf("\n%4d",temp->num);
temp=temp->next;
}
}
getch();
Page No:
}
OUTPUT
MENU
1.PUSH
2.POP
3.PRINTSTACK
4.EXIT
ENTER YOUR CHOICE 1
ENTER THE NUMBER 1
THE 1 IS PUSHED ONTO STACK
MENU
1.PUSH
2.POP
3.PRINTSTACK
4.EXIT
ENTER YOUR CHOICE 1
ENTER THE NUMBER 2
Page No:
THE 2 IS PUSHED ONTO STACK
MENU
1.PUSH
2.POP
3.PRINTSTACK
4.EXIT
ENTER YOUR CHOICE 1
ENTER THE NUMBER 3
THE 3 IS PUSHED ONTO STACK
MENU
1.PUSH
2.POP
3.PRINTSTACK
4.EXIT
ENTER YOUR CHOICE 3
Page No:
THE STACK ELEMENTS ARE
3
2
1
MENU
1.PUSH
2.POP
3.PRINTSTACK
4.EXIT
ENTER YOUR CHOICE 2
THE 3 IS POPPED OFF THE STACH
MENU
1.PUSH
2.POP
3.PRINTSTACK
4.EXIT
ENTER YOUR CHOICE 3
Page No:
THE STACK ELEMENTS ARE
2
1
MENU
1.PUSH
2.POP
3.PRINTSTACK
4.EXIT
ENTER YOUR CHOICE 4
Aim: Program For Implementation Of Queue Using Linked List
Page No:
Program:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<alloc.h>
struct linkedqueue
{
int num;
struct linkedqueue *next;
};
typedef struct linkedqueue queue;
queue *front, *rear;
void insert(int);
void del();
void printqueue();
void main()
{
int choice,x;
front=rear=NULL;
Page No:
while(1)
{
clrscr();
printf("\n\t\tMENU");
printf("\n\t1.INSERT");
printf("\n\t2.DELETE");
printf("\n\t3.PRINTQUEUE");
printf("\n\t4.EXIT");
printf("\nENTER YOUR CHOICE");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("\n ENTER THE NUMBER");
scanf("%d",&x);
insert(x);
break;
case 2: del();
break;
case 3: printqueue();
break;
case 4: exit(0);
break;
default:printf("\n INVALID CHOICE SELECTED");
Page No:
getch();
}
}
}
void insert(int x)
{
queue *newnode;
if((newnode=(queue*)malloc(sizeof(queue)))==NULL)
{
printf("\n THERE IS MEMORY TO ALLOCATE");
getch();
return;
}
newnode->num=x;
newnode->next=NULL;
if(rear==NULL)
{
rear=newnode;
front=rear;
}
else
{
Page No:
rear->next=newnode;
rear=newnode;
}
printf("\n THE %d IS INSERTED INTO QUEUE",newnode->num);
getch();
}
void del()
{
queue *temp;
temp=front;
if(temp==NULL)
printf("\n THE QUEUE IS EMPTY");
else
{
front=temp->next;
printf("\n THE %d IS DELETED FROM QUEUE",temp->num);
free(temp);
if(front==NULL)
rear=front;
}
getch();
Page No:
}
void printqueue()
{
queue *temp;
if(front==NULL)
printf("\n THERE ARE NO ELEMENTS IN QUEUE");
else
{
printf("\n THE QUEUE ELEMENTS ARE");
temp=front;
while(temp!=NULL)
{
printf("%4d",temp->num);
temp=temp->next;
}
}
getch();
}
OUTPUT
Page No:
MENU
1.INSERT
2.DELETE
3.PRINTQUEUE
4.EXIT
ENTER YOUR CHOICE 1
ENTER THE NUMBER 1
THE 1 IS INSERTED INTO QUEUE
MENU
1.INSERT
2.DELETE
3.PRINTQUEUE
4.EXIT
ENTER YOUR CHOICE 1
ENTER THE NUMBER 2
THE 2 IS INSERTED INTO QUEUE
MENU
1.INSERT
2.DELETE
3.PRINTQUEUE
4.EXIT
ENTER YOUR CHOICE 1
Page No:
ENTER THE NUMBER 3
THE 3 IS INSERTED INTO QUEUE
MENU
1.INSERT
2.DELETE
3.PRINTQUEUE
4.EXIT
ENTER YOUR CHOICE 3
THE QUEUE ELEMENTS ARE 1 2 3
MENU
1.INSERT
2.DELETE
3.PRINTQUEUE
4.EXIT
ENTER YOUR CHOICE 2
THE 1 IS DELETED FROM QUEUE
MENU
1.INSERT
Page No:
2.DELETE
3.PRINTQUEUE
4.EXIT
ENTER YOUR CHOICE 3
THE QUEUE ELEMENTS ARE 2 3
MENU
1.INSERT
2.DELETE
3.PRINTQUEUE
4.EXIT
ENTER YOUR CHOICE 5
INVALID CHOICE SELECTED
MENU
1.INSERT
2.DELETE
3.PRINTQUEUE
4.EXIT
ENTER YOUR CHOICE 4
Page No:
Page No:
Page No:
Page No:
Page No:
Page No:
Page No:
Page No:
Page No:
Download