1.
Write a C program to implement array operations insert, delete, reveres
and display.
#include<stdio.h>
#include<conio.h>
void main()
{
int A[10],i,n,item,loc,loc1;
clrscr();
printf("Enter the size of an array:\n");
scanf("%d",&n);
printf("Enter the elements of an array:\n");
for(i=1;i<=n;i++)
scanf("%d",&A[i]);
printf("Enter the item to be insert:\n");
scanf("%d",&item);
printf("Enter the loc\n");
scanf("%d",&loc);
for(i=n;i>=loc;i--)
{
A[i+1]=A[i];
}
A[loc]=item;
printf("The array after insertion is\n");
for(i=1;i<=n+1;i++)
printf("%d\n",A[i]);
printf("Enter the loc to be delete;\n");
scanf("%d",&loc1);
for(i=loc1;i<=n;i++)
{
A[i]=A[i+1];
}
printf("The array after the deletion:\n");
for(i=1;i<=n;i++)
printf("%d\n",A[i]);
printf("The reversed array is:\n");
for(i=n;i>=1;i--)
printf("%d\n",A[i]);
getch();
}
Output:
Enter the size of an array:
5
Enter the elements of an array:
11
12
13
14
15
Enter the item to be insert:
100
Enter the loc
2
The array after insertion is
11
100
12
13
14
15
Enter the loc to be delete;
4
The array after the deletion:
11
100
12
14
15
The reversed array is
15
14
12
100
11
2.Write a C program to read the marks scored by n students and to find
the first 3 highest marks without sorting the list.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],n,i,h1,h2,h3,largest=0;
clrscr();
printf("Enter the number of students:\n");
scanf("%d",&n);
printf("Enter the marks of %d students:\n",n);
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
if(a[i]>largest)
largest=a[i];
}
h1=largest;
largest=0;
for(i=1;i<=n;i++)
{
if(a[i]>largest&&a[i]<h1)
largest=a[i];
}
h2=largest;
largest=0;
for(i=1;i<=n;i++)
{
if(a[i]>largest&&a[i]<h1&&a[i]<h2)
largest=a[i];
}
h3=largest;
printf("Firsthighest=%d\nSecondhighest=%d\nThird highest=%d\n",h1,h2,h3);
getch();
}
Output:
Enter the number of students:
5
Enter the marks of 5 students:
45
75
89
92
65
First highest=92
Second highest=89
Third highest=75
3. Write a C program to implement dynamic array find smallest and
largest element of the array.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int i,num;
float *data;
clrscr();
printf("Enter the number of elements\n");
scanf("%d",&num);
data=(float*)calloc(num,sizeof(float));
if(data==NULL)
{
printf("Error !!! memory not allowed");
exit(0);
}
for(i=0; i<num; i++)
{
printf("Enter the number %d",i+1);
scanf("%f",data+i);
}
for(i=1;i<num;++i)
{
if(*data>*(data+i))
*data=*(data+i);
}
printf("Smallest element=%2f",*data);
for(i=1;i<num;++i)
{
if(*data<*(data+i))
*data=*(data+i);
}
printf("Largest element=%2f",*data);
getch();
}
Output:
Enter the number of elements:
5
Enter the number 1:65
Enter the number 2:34
Enter the number 3:78
Enter the number 4:56
Enter the number 5:90
Smallest element=34.000000
Largest element=90.00000
4. Write a C program to read the names of cities and arrange them
alphabetical order.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[20][20],temp[20];
int i,j,n;
clrscr();
printf("Enter the number of city\n");
scanf("%d",&n);
printf("Enter the name of city\n");
for(i=0;i<n;i++)
scanf("%s",&str[i]);
for(i=0;i<=n;i++)
for(j=i+1;j<=n;j++)
if(strcmp(str[i],str[j])>0)
{
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
printf("The elements after sorting are\n");
for(i=0;i<=n;i++)
printf("%s\n",str[i]);
getch();
}
Output:
Enter the number of city
3
Enter the name of city
Hubli
Mysore
Arasikere
The elements after sorting are
Arasikere
Hubli
Mysore
5. Write a C program to sort the given list using the selection sort
technique.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,a[10],n,temp,loc,j,k,min;
clrscr();
printf("enter the size of an array\n");
scanf("%d",&n);
printf("enter the elements of an array\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
for(j=1;j<=n;j++)
{
min=a[j];
loc=j;
for(k=j+1;k<=n;k++)
if(min>a[k])
{
min=a[k];
loc=k;
temp=a[j];
a[j]=a[loc];
a[loc]=temp;
}
}
printf("the element after sorting are\n");
for(i=1;i<=n;i++)
printf("%d\n",a[i]);
getch();
}
Output
Enter the size of an array
5
Enter the elements of an array
66
34
24
22
11
The element after sorting are
11
22
24
34
66
6. Write a C program to sort the given list using bubble sort technique.
#include<stdio.h>
#include<conio.h>
void main()
{
int A[10],i,j,n,temp;
clrscr();
printf("enter the size of an array\n");
scanf("%d",&n);
printf("enter the elements of an array\n");
for(i=1;i<=n;i++)
scanf("%d",&A[i]);
for(i=1;i<=n-1;i++);
for(j=1;j<=n-i;j++);
if(A[j]>A[j+1])
{
temp=A[j];
A[j]=A[j+1];
A[j+1]=temp;
}
printf("enter the elements after sorting is\n");
for(i=1;i<=n;i++)
printf("%d\n",A[i]);
getch();
}
Output:
Enter the size of an array
5
Enter the elements of an array
2
3
4
1
5
Enter the elements after sorting is
1
2
3
4
5
7. Write a C program to sort the given list using insertion sort technique.
#include <stdio.h>
#include <conio.h>
void main()
{
int i,n,a[10],temp,ptr;
clrscr();
printf("Enter the size of an array:\n");
scanf("%d",&n);
printf("Enter the elements of an array:\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
a[0]=0;
for(i=2;i<=n;i++)
{
temp=a[i];
ptr=i-1;
while(temp<a[ptr])
{
a[ptr+1]=a[ptr];
ptr=ptr-1;
}
a[ptr+1]=temp;
}
printf("The elements after sorting array:\n");
for(i=1;i<=n;i++)
printf("%d\n",a[i]);
getch();
}
Output :
Enter the size of an array:
5
Enter the elements of an array:
17
13
16
12
11
The elements after sorting array:
11
12
13
16
17
8. Write the C program to sort the given list by uisng Quick sort
technique.
#include<stdio.h>
#include<conio.h>
void quick(int arr[],int low,int up);
int partition(int arr[],int low,int up);
void main()
{
int array[10],n,i;
clrscr();
printf("enter the number of elements:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the elements %d:",i+1);
scanf("%d",&array[i]);
}
quick(array,0,n-1);
printf("sorted list is:\n");
for(i=0;i<n;i++)
printf("%d\n",array[i]);
printf("\n");
getch();
}
int partition(int arr[],int low,int up)
{
int temp,i,j,pivot;
i=low+1;
j=up;
pivot=arr[low];
while(i<=j)
{
while((arr[i]<pivot)&&(i<up))
i++;
while((arr[j])>pivot)
j--;
if(i<j)
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
i++;
j--;
}
else
i++;
}
arr[low]=arr[j];
arr[j]=pivot;
return j;
}
void quick(int arr[],int low,int up)
{
int pivotc;
if(low>=up)
return;
pivotc=partition(arr,low,up);
quick(arr,low,pivotc-1);
quick(arr,pivotc+1,up);
}
Output
enter the number of elements:5
enter the elements 1:67
enter the elements 2:54
enter the elements 3:98
enter the elements 4:22
enter the elements 5:66
sorted list is:
22
54
66
67
98
9. Write a C program sort the given list by using Merge sort techniques.
#include <stdio.h>
#include <conio.h>
void main()
{
int v[10],i,j,k,n;
void mergepass();
void mergsort();
clrscr();
printf("\n\n\n\n\n\t OUTPUT FOR MERGE SORT \n\n");
printf("Enter the size of an array:\n");
scanf("%d",&n);
printf("Enter the elements of array:\n");
for(i=0;i<n;i++)
scanf("%d",&v[i]);
i=0;
mergepass(v,i,n-1);
printf("The merge sorted array:\n");
for(i=0;i<n;i++)
printf("%d\n",v[i]);
getch();
}
void mergesort(I,top,size,pot)
int I[10],top,size,pot;
{
int u,f1,s1,t1,temp[10];
f1=top;
s1=size+1;
t1=top;
while((f1<=size)&&(s1<=pot))
{
if(I[f1]<=I[s1])
{
temp[t1]=I[f1];
f1++;
}
else
{
temp[t1]=I[s1];
s1++;
}
t1++;
}
if(f1<=size)
{
for(f1=f1;f1<=size;f1++)
{
temp[t1]=I[f1];
t1++;
}
}
else
{
for(s1=s1;s1<=pot;s1++)
{
temp[t1]=I[s1];
t1++;
}
}
for(u=top;u<=pot;u++)
I[u]=temp[u];
}
void mergepass(a,m,n)
int a[10],m,n;
{
int h;
if(m!=n)
{
h=(m+n)/2;
mergepass(a,m,h);
mergepass(a,h+1,n);
mergesort(a,m,h,n);
}
}
Output
Enter the size of an array:
5
Enter the elements of array:
13
18
11
14
19
The merge sorted array:
11
13
14
18
19
10. Write a C program to seach an element by using Linear Search
Techniques.
#include<stdio.h>
#include<conio.h>
void main()
{
int A[10],n,i,item,loc;
clrscr();
printf("Enter size of an array\n");
scanf("%d",&n);
printf("Enter the elements of an array\n");
for(i=1;i<=n;i++)
scanf("%d",&A[i]);
printf("Enter the item to be search");
scanf("%d",&item);
for(i=1;i<=n;i++)
if(A[i]==item)
loc=i;
printf("search is successful at loc %d",loc);
if(loc==0)
printf("search is not found ");
getch();
}
Enter size of an array
5
Enter the elements of an array
11
12
13
14
15
Enter the item to be search
13
Search is successful at loc 3
11. Write a C program to search an element by using Binary Search
techniques using Recursion.
#include<stdio.h>
#include<conio.h>
int n,key,a[10],mid;
void main()
{
int i,ans,low,high;
clrscr();
printf("Enter size of an array\n");
scanf("%d",&n);
printf("Enter elements of an array\n");
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the key element to be search\n");
scanf("%d\n",&key);
low=1;
high=n;
ans=binsrch(low,high);
if(ans==1)
{
printf("The search is successful at location %d\n",mid);
}
else
{
printf("The search is unsuccessful");
}
getch();
}
int binsrch(int low,int high)
{
if(low>high)
{
return 0;
}
mid=((low+high)/2);
if(key==a[mid])
{
return 1;
}
if(key<a[mid])
{
return binsrch(low,mid+1);
}
if(key>a[mid])
{
return binsrch(mid+1,high);
}
return 0;
}
Output 1 :
Enter size of an array
5
Enter elements of an array
22
55
66
77
900
Enter the key element to be search
77
The search is successful at location 4
Output 2 :
Enter size of an array
5
Enter elements of an array
22
55
66
77
900
Enter the key element to be search
600
The search is unsuccessful
12. Write a C program to implement Tower of Hanoi.
#include<stdio.h>
#include<conio.h>
void tofh(int ndisc,char source,char temp, char dest);
void main()
{
char source='A',temp='B',dest='C' ;
int ndisc;
clrscr();
printf("Enter the no of discs\n");
scanf("%d",&ndisc);
tofh(ndisc,source,temp,dest);
getch();
}
void tofh(int ndisc, char source, char temp,char dest)
{
if(ndisc==1)
{
printf("move disc %d from %c to %c\n",ndisc,source,dest);
return;
}
tofh(ndisc-1,source,dest,temp);
printf("move disc %d from %c to %c\n",ndisc,source,dest);
tofh(ndisc-1,temp,source,dest);
}
Output
Enter the no of discs
3
move disc 1 from A to C
move disc 2 from A to B
move disc 1 from C to B
move disc 3 from A to C
move disc 1 from B to A
move disc 2 from B to C
move disc 1 from A to C
13. Write a C program to display Pascal Triangle.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,c,s,i,j,k;
int nr,cf=1;
clrscr();
printf("Enter number of rows\n");
scanf("%d",&nr);
for(i=0;i<nr;i++)
{
for(k=1;k<=nr-i;k++)
printf(" ");
for(j=0;j<=i;j++)
{
if(j==0||i==0)
cf=1;
else
cf=cf*(i-j+1)/j;
printf("%4d",cf);
}
printf("\n");
}
}
Output:
Enter number of rows
6
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
14. C Program to find the value of ex up to 4 decimal places using infinite
series.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int x,i,p;
double fact=1.0,sum=1.0;
clrscr();
printf("Enter the value of x\n");
scanf("%d",&x);
for(i=1;i<=4;i++)
{
fact=(float)fact*i;
p=pow(x,i);
sum=sum+(p/fact);
}
printf("The exponentiation is %.15lf\n",sum);
getch();
}
Output :
Enter the value of x
The exponentiation is 2.708333333333333
15. Write a C program to implement Stack.
#include<stdio.h>
# define size 5
int stack[10];
int top=-1,choice;
void main()
{
int rpt=1;
clrscr();
printf("stack operation\n");
while (rpt)
{
printf("enter your choice\n");
printf("1.push 2.pop 3.display 4.exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:exit(1);
default: printf("illigal entry\n");
printf("do you want to continue type 0 or 1\n");
scanf("%d",&rpt);
}
}
getch();
}
push()
{
int item;
if(top==size-1)
{
printf("stack is over flow\n");
}
else
{
printf("enter the item to be insert\n");
scanf("%d",&item);
top=top+1;
stack[top]=item;
}
return;
}
pop()
{
int num;
if(top==-1)
{
printf("stack is empty\n");
return;
}
else
{
num=stack[top];
printf("the item delete is %d\n",num);
top=top-1;
}
return;
}
display()
{
int i;
if(top==-1)
{
printf("stack is empty\n");
}
else
{
printf("\n the status of the stack is\n");
for(i=top;i>=0;i--)
{
printf("%d\n",stack[i]);
}
}
printf("\n"); return; }
Output :
enter your choice
1.push 2.pop 3.display 4.exit
1
enter the item to be insert
11
do you want to continue type 0 or 1
1
enter your choice
1.push 2.pop 3.display 4.exit
1
enter the item to be insert
12
do you want to continue type 0 or 1
1
enter your choice
1.push 2.pop 3.display 4.exit
1
enter the item to be insert
13
do you want to continue type 0 or 1
1
enter your choice
1.push 2.pop 3.display 4.exit
3
the status of the stack
13
12
11
do you want to continue type 0 or 1
1
enter your choice
1.push 2.pop 3.display 4.exit
2
the item delete is
13
do you want to continue type 0 or 1
1
enter your choice
1.push 2.pop 3.display 4.exit
3
the status of the stack
12
11
do you want to continue type 0 or 1
1
enter your choice
1.push 2.pop 3.display 4.exit
2
the item delete is
12
do you want to continue type 0 or 1
1
enter your choice
1.push 2.pop 3.display 4.exit
2
the item delete is
11
do you want to continue type 0 or 1
1
enter your choice
1.push 2.pop 3.display 4.exit
3
stack is empty
16. Write a C program to implement simple queue.
#include<stdio.h>
#include<conio.h>
#define size 5
int rear=-1,front=-1,queue[size];
void main()
{
int choice;
int queue[size];
int rpt=1;
clrscr();
while(rpt)
{
printf("Enter your choice 1.qinsert 2.qdelete 3.display:\n");
scanf("%d",&choice);
switch(choice)
{
case 1:qinsert();
break;
case 2:qdelete();
break;
case 3:qdisplay();
break;
default:printf("Enter the correct choice:\n");
break;
}
printf("If you want to continue yes type 1 or 0:\n");
scanf("%d",&rpt);
}
getch();
}
qinsert()
{
int item;
if(rear==size-1)
{
printf("The queue is full:\n");
}
else
{
printf("Enter the item to be insert:\n");
scanf("%d",&item);
rear=rear+1;
queue[rear]=item;
}
if(front==-1)
{
front=front+1;
}
return;
}
qdelete()
{
int item;
if(front==-1||front>rear)
{
printf("The queue is empty:\n");
}
else
{
item=queue[front];
printf("The deleted item is %d:\n",item);
front=front+1;
}
return;
}
qdisplay()
{
int i;
if(front==-1||front>rear)
{
printf("The Queue is empty:\n");
}
else
{
printf("The elements of queue are:\n");
for(i=front;i<=rear;i++)
printf("%d\n",queue[i]);
}
return;
}
Output
Enter your choice 1.qinsert 2.qdelete 3.display
1
Enter the item to be insert
11
If you want to continue yes type 1 or 0
1
Enter your choice 1.qinsert 2.qdelete 3.display
1
Enter the item to be insert
12
If you want to continue yes type 1 or 0
1
Enter your choice 1.qinsert 2.qdelete 3.display
1
Enter the item to be insert
13
If you want to continue yes type 1 or 0
1
Enter your choice 1.qinsert 2.qdelete 3.display
3
The elements of queue are
11
12
13
If you want to continue yes type 1 or 0
1
Enter your choice 1.qinsert 2.qdelete 3.display
2
The deleted item is 11
If you want to continue yes type 1 or 0
3
The elements of queue are
12
13
17. Write a C program to implement circular queue.
#include<stdio.h>
#include<conio.h>
# define size 3
int rear=-1,front=-1,CQ[size];
void main()
{
int choice,Q[size],rpt=1;
clrscr();
while(rpt)
{
printf("enter your choice 1:Qinsert 2:Qdelete 3:display\n");
scanf("%d",&choice);
switch(choice)
{
case 1:CQinsert();
break;
case 2:CQdelete();
break;
case 3:CQdisplay();
break;
default:printf("enter the currect choice\n");
break;
}
printf("do you want to continue if yes print 1 or 0\n");
scanf("%d",&rpt);
}
getch();
}
CQinsert()
{
int num;
if(front==(rear+1)%size)
{
printf("Q is full");
}
else
{
printf("enter the item to be insert\n");
scanf("%d",&num);
if (front==-1)
{
front=0,rear=0;
}
else if(rear==size-1)
{
rear=0;
}
else
rear=(rear+1)%size;
CQ[rear]=num;
printf("front=%d rear=%d",front,rear);
}
return;
}
CQdelete()
{
int num;
if(front==-1)
{
printf("circular quee is empty");
}
else
{
num=CQ[front];
CQ[front]=0;
printf("the deleted element is %d\n",num);
if(front==rear)
{
front=0;
}
else
front=(front+1)%size;
}
return;
}
CQdisplay()
{
int i;
if(front==-1)
{
printf("Q is empty cannot be display\n");
}
else
{
printf("the CQ elements are\n");
for(i=0;i<size;i++)
printf("%d\n",CQ[i]);
}
return;
}
Output:
enter your choice 1:Qinsert 2:Qdelete 3:display
1
enter the item to be insert
11
front=0 rear=0do you want to continue if yes print 1 or 0
1
enter your choice 1:Qinsert 2:Qdelete 3:display
1
enter the item to be insert
22
front=0 rear=1do you want to continue if yes print 1 or 0
1
enter your choice 1:Qinsert 2:Qdelete 3:display
1
enter the item to be insert
33
front=0 rear=2do you want to continue if yes print 1 or 0
1
enter your choice 1:Qinsert 2:Qdelete 3:display
3
the CQ elements are
11
22
33
do you want to continue if yes print 1 or 0
1
enter your choice 1:Qinsert 2:Qdelete 3:display
2
the deleted element is 11
do you want to continue if yes print 1 or 0
1
enter your choice 1:Qinsert 2:Qdelete 3:display
1
enter the item to be insert
44
do you want to continue if yes print 1 or 0
1
enter your choice 1:Qinsert 2:Qdelete 3:display
3
the CQ elements are
22
33
44
do you want to continue if yes print 1 or 0
0
18. Write a C program to implement linear linked list.
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct list
{
int info;
struct list *next;
};
typedef struct list *NODE;
NODE insertfront(NODE,int);
NODE insertafter(NODE,int,int);
NODE insertlast(NODE,int);
NODE deletenode(NODE,int);
int searchnode(NODE,int);
void display(NODE);
void main()
{
int choice,rpt=1;
int e,x;
NODE p;
p=NULL;
clrscr();
while(rpt==1)
{
printf("1.insertfront 2.insertmiddle 3.insertlast 4.delete 5.search 6.display
7.exit\n");
printf("Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("Enter the element value:");
scanf("%d",&e);
p=insertfront(p,e);
break;
case 2:printf("Enter the element value:");
scanf("%d",&e);
printf("Enter the key value:");
scanf("%d",&x);
p=insertafter(p,x,e);
break;
case 3:printf("Enter the element value:");
scanf("%d",&e);
p=insertlast(p,e);
break;
case 4:printf("Enter the element value:");
scanf("%d",&x);
p=deletenode(p,x);
break;
case 5:printf("Enter the element value:");
scanf("%d",&x);
if(searchnode(p,x))
printf("NODE found\n");
else
printf("NODE not found\n");
break;
case 6:display(p);
break;
case 7:printf("Do u want to continue type 0 or 1");
scanf("%d",&rpt);
break;
default:printf("Invalid choice\n");
break;
}
}
getch();
}
NODE insertfront(NODE p,int e)
{
NODE q;
q=(NODE)malloc(sizeof(struct list));
q->info=e;
q->next=p;
p=q;
return p;
}
NODE insertafter(NODE p,int x,int e)
{
NODE q,k;
k=p;
while(k!=NULL&&k->info!=x)
k=k->next;
if(k==NULL)
printf("key node not found\n");
else
{
q=(NODE)malloc(sizeof(struct list));
q->info=e;
q->next=k->next;
k->next=q;
}
return p;
}
NODE insertlast(NODE p,int e)
{
NODE q,k;
k=p;
if(p==NULL) return p;
q=(NODE)malloc(sizeof(struct list));
q->info=e;
q->next=NULL;
while(k->next!=NULL)
k=k->next;
k->next=q;
return p;
}
NODE deletenode(NODE p,int x)
{
NODE k;
NODE pred;
pred=NULL;
k=p;
while(k!=NULL && k->info!=x)
{
pred=k;
k=k->next;
}
if(k==NULL)
printf("NODE not found can't delete");
else
if(pred==NULL)
k=k->next;
else
pred->next=k->next;
return p;
}
int searchnode(NODE p,int x)
{
NODE k=p;
while(k!=NULL&&k->info!=x)
k=k->next;
if(k==NULL)
return 0;
else
return 1;
}
void display(NODE p)
{
while(p!=NULL)
{
printf("%d\n",p->info);
p=p->next;
}
}
Output:
1.insertfront 2.insertmiddle 3.insertlast 4.delete 5.search 6.display 7.exit
Enter your choice:1
Enter the element value:10
1.insertfront 2.insertmiddle 3.insertlast 4.delete 5.search 6.display 7.exit
Enter your choice:3
Enter the element value:20
1.insertfront 2.insertmiddle 3.insertlast 4.delete 5.search 6.display 7.exit
Enter your choice:2
Enter the element value:15
Enter the key value:10
1.insertfront 2.insertmiddle 3.insertlast 4.delete 5.search 6.display 7.exit
Enter your choice:3
Enter the element value:25
1.insertfront 2.insertmiddle 3.insertlast 4.delete 5.search 6.display 7.exit
Enter your choice:3
Enter the element value:30
1.insertfront 2.insertmiddle 3.insertlast 4.delete 5.search 6.display 7.exit
Enter your choice:6
10
15
20
25
30
1.insertfront 2.insertmiddle 3.insertlast 4.delete 5.search 6.display 7.exit
Enter your choice:4
Enter the element value:15
1.insertfront 2.insertmiddle 3.insertlast 4.delete 5.search 6.display 7.exit
Enter your choice:6
10
20
25
30
1.insertfront 2.insertmiddle 3.insertlast 4.delete 5.search 6.display 7.exit
Enter your choice:5
Enter the element value:10
NODE found
1.insertfront 2.insertmiddle 3.insertlast 4.delete 5.search 6.display 7.exit
Enter your choice:7
Do u want to continue type 0 or 1
19. Write a C program to create a two files to store even and odd numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *f1, *f2;
int A[10],n,i;
clrscr();
printf("Enter the size of an array:\n");
scanf("%d",&n);
printf("Enter the elements of an array:\n");
for(i=1;i<=n;i++)
scanf("%d",&A[i]);
f1=fopen("odd.txt","w");
f2=fopen("even.txt","w");
for(i=1;i<=n;i++)
{
if(A[i]%2==0)
{
fprintf(f2,"%d\n",A[i]);
}
else
{
fprintf(f1,"%d\n",A[i]);
}
}
fclose(f1);
fclose(f2);
getch();
}
Output
Enter the size of an array:
6
Enter the elements of an array:
11
13
14
16
18
12
20. Write a C program to create a file to store student records.
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
int regno;
char name[20];
char cls[20];
float avg;
clrscr();
fp=fopen("std.txt","w");
if(fp==NULL)
{
printf("File does not exist:\n");
return;
}
printf("Enter the regno:\n");
scanf("%d",®no);
fprintf(fp,"regno=%d\n",regno);
printf("Enter the student name:\n");
scanf("%s",name);
fprintf(fp,"name=%s\n",name);
printf("Enter the cls:\n");
scanf("%s",cls);
fprintf(fp,"class=%s\n",cls);
printf("Enter the percentage:\n");
scanf("%f",&avg);
fprintf(fp,"per=%f\n",avg);
fclose(fp);
getch();
}
Enter the regno:
121
Enter the student name:
Pruthvi
Enter the cls:
Bsc
Enter the percentage:
80
21. Write a C program to convert infix to postfix expression.
#include<stdio.h>
#include<conio.h>
#include<string.h>
char infix[20],postfix[20],stack[20];
int top=-1,index=0,pos=0,length;
char symbol,temp;
void main()
{
clrscr();
printf("Enter the infix expression");
scanf("%s",infix);
infix_to_postfix(infix,postfix);
printf("The infix expression is %s\n",infix);
printf("The postfix expression is %s\n",postfix);
getch();
}
infix_to_postfix(char infix[],char postfix[])
{
length=strlen(infix);
push('#');
while(index<length)
{
symbol=infix[index];
switch(symbol)
{
case'(':push(symbol);
break;
case')':temp=pop(symbol);
while(temp!='(')
{
postfix[pos]=temp;
pos++;
temp=pop();
}
break;
case'+':
case'-':
case'*':
case'/':
case'^':
while(preced(stack[top])>=preced(symbol))
{
temp=pop();
postfix[pos]=temp;
pos++;
}
push(symbol);
break;
default:postfix[pos++]=symbol;
}
index++;
}
while(top>0)
{
temp=pop();
postfix[pos++]=temp;
}
return;
}
push(char symbol)
{
top=top+1;
stack[top]=symbol;
return;
}
pop()
{
char symbol;
symbol=stack[top];
top=top-1;
return symbol;
}
int preced(char symbol)
{
int p;
switch(symbol)
{
case'^':p=3;
break;
case'*':p=2;
break;
case'/':p=2;
break;
case'+':p=1;
break;
case'-':p=1;
break;
case'(':p=0;
case')':
break;
case'#':p=-1;
break;
}
return(p);
}
Output:
Enter the infix expression
a+b(c-d)/e*f
The infix expression is a+b(c-d)/e*f
The postfix expression is abcd-e/f*+
22. Write a C program to display Traversal of a Tree.
#include<stdio.h>
#include<malloc.h>
struct tree
{
int info;
struct tree *left;
struct tree*right;
};
typedef struct tree*Tnode;
Tnode createtree(Tnode,int);
void preorder(Tnode);
void inorder(Tnode);
void postorder(Tnode);
void main()
{
char e;
int choice,done;
Tnode p;
p=NULL;
done=0;
clrscr();
while(!done)
{
printf("enter your choice 1.createtree 2.preorder 3.inorder 4.postorder
5.exit\n");
printf("enter choice\n");
scanf("%d",&choice);
switch (choice)
{
case 1:printf("enter the element\n");
scanf("%d",&e);
p=createtree(p,e);
break;
case 2:preorder(p);
break;
case 3:inorder(p);
break;
case 4:postorder(p);
break;
case 5:done=1;
break;
default:printf("invalid choice\n");
break;
}
}
}
Tnode createtree(Tnode p,int e)
{
if(p==NULL)
{
p=(Tnode)malloc(sizeof(struct tree));
p->info=e;
p->left=p->right=NULL;
}
else
if(e==p->info)
printf("duplicate entry\n");
else
if(e<p->info)
p->left=createtree(p->left,e);
else
p->right=createtree(p->right,e);
return p;
}
void preorder(Tnode p)
{
if(p!=NULL)
{
printf("%d",p->info);
printf("/n");
preorder(p->left);
preorder(p->right);
}
}
void inorder(Tnode p)
{
if(p!=NULL)
{
inorder(p->left);
printf("%d",p->info);
printf("/n");
inorder(p->right);
}
}
void postorder(Tnode p)
{
if(p!=NULL)
{
postorder(p->left);
postorder(p->right);
printf("%d",p->info);
printf("/n");
}
}
Output
1. Create 2.preorde 3.inorder 4.postorder 5.exit
Enter choice: 1
Enter the element: 10
1. Create .2preorder 3.inorder 4.postorder 5.exit
Enter choice: 1
Enter the element: 20
1. Create 2.preorder 3.inorder 4.postorder 5.exit
Enter choice: 1
Enter the element: 15
1. Create 2.preorder 3.inorder 4.postorder 5.exit
Enter choice: 1
Enter the element: 5
1. Create 2.preorder 3.inorder 4.postorder 5.exit
Enter choice: 1
Enter the element: 50
1. Create 2.preorder 3.inorder 4.postorder 5.exit
Enter choice: 2
10 5 20 15 50
1. Create 2.preorder 3.inorder 4.postorder 5.exit
Enter choice: 3
5 10 15 20 50
1. Create 2.preorder 3.inorder 4.postorder 5.exit
Enter choice: 4
5 15 50 20 10
1. Create 2.preorder 3.inorder 4.postorder 5.exit
Enter choice: 5