/*1.
C program to find the GCD using recursive function */
#include<stdio.h>
#include<conio.h>
int gcd(int n1, int n2);
void main()
{
int n1,n2 g;
clrscr();
printf("\n Enter two numbers \n");
scanf("%d%d",&n1,&n2);
g=gcd(n1, n2);
printf(“GCD of %d and %d is:%d”,n1,n2;g);
}
int gcd(int n1, int n2);
{
if(n2!=0)
return(gcd(n2, nl%n2));
else
return(nl);
OUTPUT
Enter two numbers
4
8
GCD of 4 and 8 is:4
Enter two numbers
5
42
GCD of 5 and 12 is:1
/*2. C program to display Pascal Triangle using binomial
function*/
#include<stdio.h>
#include<conio.h>
void main()
{
int rows,c=1,blk,i,j;
clrscr();
printf("Enter the number of rows:");
scanf("%d",&rows);
for(i=0;i<rows;i++)
{
for(blk=1;b1k<=rows-i;b1k++)
printf(" ");
for(j=0;j<=i;j++)
{
if(j..elli..0)
c=1;
else
c=c*(i-j+1)/j;
printf("%4d",c);
}
printf("\n");
}
}
OUTPUT
Enter the number of rows: 5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
/*3.C program to generate N Fibonacci numbers using recursive
function*/
#include <stdio.h>
#include<conio.h>
int fibo(int n)
{
if(n==0)
return 0;
else
if(n==1)
return 1;
else
return(fibo(n-1)+fibo(n-2));
}
void main( )
{
int i,n;
clrscr();
printf("\n Enter the number of terms :");
scanf("%d",&n);
printf("\n Fibonacci series \n");
for(i=1;i<=n;i++)
printf("%d \n",fibo(i));
}
OUTPUT
Enter the number of terms :10
Fibonacci series
1
1
2
3
5
8
13
21
34
55
/* 4. C program to implement Tower of Hanoi */
#include<stdio.h>
#include<conio.h>
void ToH(int n, char rod1, char rod2, char rod3)
{
if(n==1)
{
printf("\n Move disk 1 from rod %c to rod %c",rodl,rod2);
return;
}
ToH(n-1, rod1, rod3, rod2);
printf("\n Move disk %d from rod %c to rod %c", n,rodl,rod2);
ToH(n-1, rod3, rod2, rod1);
}
void main()
{
int n=3;
clrscr();
ToH(n, ‘A’,‘C’,’B’);
}
OUTPUT
Move disk 1 from rod A to rod C
Move disk 2 from rod A to rod B
Move disk 1 from rod C to rod B
Move disk 3 from rod A to rod C
Move disk 1 from rod B to rod A
Move disk 2 from rod B to rod C
Move disk 1 from rod A to rod C
/* 5.C program to find the smallest and largest element of an
array*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,n,big,small;
clrscr();
printf("Enter the size of the array:");
scanf("%d",&n);
printf("Enter %d elements\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
big=small-a[0];
for(i=1;i<n;i++)
{
if(a[i]>big)
big=a[i];
if(a[i]<small)
small=a[i];
}
printf("Largest element=%d\n",big);
printf("Smallest element=%d\n",small);
}
OUTPUT
Enter the size of the array:6
Enter 6 elements
12 45
78 10
54 5
Largest element=78
Smallest element=5
/* 6.C program to arrange the city names alphabetically */
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,j;
char str[20][20],s[20];
clrscr();
printf("\n Enter the Number of Cities:\n");
scanf("%d",&n);
printf("Enter %d City Names\n",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(s,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],s);
}
}
}
printf("City Name in Alphabetical Order:\n");
for(i=0;i<n;i++)
printf("%s\n",str[i]);
}
OUTPUT
Enter the Number of Cities:6
Enter 6 City Names
Kalaburagi
Raichur
Bidar
Yadgir
Hubballi
City Name in Alphabetical Order:
Bidar
Hubballi
Kalaburagi
Raichur
Yadgir
/* 7.0 program to sort the given list using selection sort
method*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[15],i,j,k,n,pass,min; clrscr();
printf("Enter the size of the array:");
scanf("%d",&n);
printf("Enter %d elements\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Array elements are:\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
/*selection sort begins*/
for(pass=0;pass<n-1;pass++)
{
min=pass;
for(j=pass+1;j<n;j++)
{
if(a[min]>a[j]) min=j;
}
if(min!=pass)
{
k=a[pass];
a[pass]=a[min];
a[min]=k;
}
}
printf("\n Sorted elements of array are:\n"); for(i=0;i<n;i+
+)
printf("%d\t",a[i]);
}
OUTPUT
Enter the size of the array:6
Enter 6 elements
35 25 15 78 65 54
Array elements are:
35 25 15 78 65 54
Sorted elements of array are:
15 25 35 54 65 78
/* 8.0 program to sort the given list using bubble sort
method*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[15],n,i,j,t;
clrscr();
printf("Enter the size of the array:");
scanf("%d",&n);
printf("Enter %d elements\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Array elements are:\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
/*bubble sort begins*/
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
1
}
1
printf("\n Sorted elements of array are:\n"); for(i=0;i<n;i+
+)
printf("%d\t",a[i]);
}
OUTPUT
Enter the size of the array:6
Enter 6 elements
56 45 78 25 85 16
Array elements are:
56 45 78 25 85 16
Sorted elements of array are:
16 25 45 56 78 85
/* 9.C program to sort the given list using insertion sort
method*/
#include<stdio.h>
#include<conio.h>
void insort(int [], int);
void main()
{
int a[10],i,n;
clrscr();
printf("\n Enter the size of the array:\n");
scanf("%d",&n);
printf("\n Enter %d elements\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
insort(a,n);
}
/* Function to sort an array using insertion sort*/
void insort(int a[],int n)
{
int i,j,t;
for(i=0;i<n;i++)
{
t=a[i];
for(j=i-1;j>=0 && t<a[j];j--)
{
a[j+1]=a[j];
} a[j+1]=t;
}
printf("\n Sorted elements after insertion Sort are:\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
}
OUTPUT
Enter the size of the array: 6
Enter 6 elements
78 25 67 56 45 16
Sorted elements after insertion Sort are:
16 25 45 56 67 78
/*10.C Program sort the given list using Quick sort method*/
#include <stdio.h>
#include<conio.h>
void sort(int a[],int lb,int ub)
{
int f=1,i,j,key,t;
if(lb<ub)
{
i=lb;
j=ub+1;
key=a[lb];
while(f)
{
i++;
while(a[i]<key && i<ub)
i++;
j--;
while(a[j]>key)
j--;
if(i<j)
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
else f=0;
}
a[lb]=a[j];
a[j]=key;
sort(a,lb,j-1);
sort(a,j+1,ub);
}
void main()
{
int a[10],i,j,k,n,pass,min;
clrscr();
printf("\n Enter the size of the array:");
scanf("%d",&n);
printf("\n Enter %d elements\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n Unsorted elements are: \n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
i=0;
sort(a,i,n-1);
printf("\n Sorted elements are:\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}
OUTPUT
Enter the size of the array:6
Enter 6 elements
58 26 16 37 78 45
Unsorted elements are:
58 26 16 37 78 45
Sorted elements are:
16 26 37 45 58 78
/*11. C program to search an element using linear search
method*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,item,n,f=0,1oc;
clrscr();
printf("Enter the size of array:");
scanf("%d",&n);
printf("Enter %d elements\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the item to be searched:");
scanf("%d",&item);
for(i=0;i<n;i++)
{
if(item==a[i])
{
f=1;
loc=i+1;
break;
}
}
if(f==1)
printf("Successful, item found in %d location \n",loc);
else
printf("Unsuccessful, item not found in the list \n");
}
OUPUT
Enter the size of array: 6
Enter 6 elements
15 78 25 64 45 56
Enter the item to be searched: 78
Successful, item found in 2 location.
/*12. C program to search an element using binary search
method*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,j,t,item,n,beg,mid,end;
clrscr();
printf("Enter the size of array:");
scanf("%d",&n);
printf("Enter %d elements\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
/* sorting the elements */
for(i=0;i<n-1;i++)
{
for(j=i+1;1<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf("\n Sorted elements of array are:\
n"); for(i=0;i<n;i++)
printf("%d\t",a[i]);
printf("\n Enter the item to be
searched:"); scanf("%d",&item);
/* binary search beging */
beg=1;
end=n;
do
{
mid=(beg+end)/2;
if(item>a[mid])
beg=mid+1;
if(item<a[mid])
end=mid-1;
}
while(item!=a[mid] && beg<=end);
if(item==a[mid])
printf("Search is successful \n");
else
printf("Search is unsucessful,item not found in the list \
n");
OUPUT
Enter the size of array: 6
Enter 6 elements
45 35 16 26
78 65
Sorted elements of array are:
16 26 35 45 65 78
Enter the item to be searched: 45
Search is successful
Enter the item to be searched: 85
Search is unsuccessful
/*13. C program to implement Stack */
#include<stdio.h>
#include<conio.h>
#define size 4
static int top=-1;
int stack[size];
void push()
{
int item;
if(top==size-1)
{
printf: ni OVERFLOW");
return;
}
printf("\nEnter the item to be pushed:");
scanf("%d",&item);
stack[++top]=item;
}
void pop()
{
int item;
if (top==0)
{
printf ("UNDERFLOW\n");
return;
}
item=stack[top--];
printf("Popped item is:%d\n",item);
}
void display()
{
int i;
if{top==-1)
{
printf("\n Stack is empty\n");
return;
}
for(i=top;i>=0;i--)
{
printf{ "%d\t” stack[i]);
}
printf(“ \n");
}
void main()
{
int choice, ch=1;
clrscr();
while(ch!=0)
{
printf(“\n 1.Push 2.Pop 3.Display \n");
printf("\n Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
default; exit(e);
}
printf("\n Press 0 for NO and 1 for YES:");
scanf("%d",&ch);
}
}
OUTPUT
1.Push 2.Pop 3 Display 1.Push 2.Pop 3 Display
Enter your choice: 1
Enter your choice: 2
Enter the item to be pushed:
Popped item is: 56 -
34
Press 0 For NO and 1 for YES: Press 0 For NO and 1 for YES:
l l
1.Push 2.Pop 3 Display
1.Push 2.Pop 3 Display
Enter your choices: 3
Enter your choice: 1
45 34
Enter the item to be pushed:
45 Press 0 For NO and 1 for YES: 0
Press 0 For NO and 1 for YES:
l
Enter your choice: 1
Enter the item to be pushed: 56
Press 0 For NO and 1 for YES: l
1.Push 2.Pop 3 Display
/*14. C program to convert an infix expression to postfix*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char ch,stack[50],infix[50];
char ans='y’;
ant 151, top=0;
clrscr();
while(ans=="y'|| ans=="Y')
{
Stack[0]="(";
printf("\n Enter An Infix Expression:");
gets(infix);
fflush(stdin);
j=strlen(infix);
printf("\n The Postfix Expression is:\n");
for(i=0;i<j;i++)
{
ch=infix[i];
if((ch>='0"&& ch<="9")||(ch>="a'&& ch<="z")||(ch>='A"&& ch>="Z"))
printf(“\n %c",ch);
if(ch==’(‘)
stack[++top]=ch;
if(ch=='*’||ch==’/’)
{
while(stack{topl=="*"||stack[top]=='/’)
printf("\n %c",stack[top--]);
stack[++top]=ch;
}
if(ch==’+’||ch==’-’)
{
while(stack[top]==’+’||stack[top]=='-’)
{
printf(“\n %c” ;stack[top]);
top--;
}
stack[++top]=ch;
}
if(ch==’)’)
{
while(stack[top]!=’(‘)
printf ("%c",stack[top--]);
top--;
}
}
while(stack[top]!=’(’)
printf("%c",stack[top--]);
printf("\n Do you want to continue(y/n)?:");
scanf("%c",&ans);
fflush(stdin);
}
}
OUTPUT
Enter an infix expression: (a+b)/(c-d)*(e+f)
The postfix expression is:
ab+cd-/ef+*
Do you want to continue(y/n)?: n
/*15.C program to implement simple Queue */
#include<stdio.h>
#include<conio.h>
#define size 5
void ginsert(int);
void qdelete();
void display();
int Queue[size],front=-1,rear=-1;
void main()
{
clEserl
ginsert(10);
ginsert(20);
ginsert(30);
ginsert(40);
ginsert(50);
display();
qdelete();
display();
}
void ginsert(int value)
{
if(rear==size-1)
printf("\n Queue Overflow");
else
{
It(front==-1)
front=0:
rear++;
Queue[rear]=value;
printf("\n Inserted ->%d",value);
}
}
void qdelete()
{
if(front==-1)
printf ("\n Queue Underflow");
else
{
printf("Deleted Element:%d",Queue[front]);
front++;
if(front>rear)
front=rear=-1;
}
}
void display()
{
int i;
if(rear==-1)
printf("\n Queue is Empty");
else
{
printf("\n Queue elements are:\n");
for(i=front;i<=rear;i++)
printf ("%d\t",Queue[i]);
}
printf(“\n”);
}
OUTPUT
Inserted ->10
Inserted ->20
Inserted ->30
Inserted ->40
Inserted ->50
Queue elements are:
10 20 30 40 50
Deleted Element: 10
Queue elements are:
20 30 40 50
/*16. C program to implement Linked list */
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct list
{
int info;
struct list “next;
};
typedef struct list node;
node *header;
/*Function to create a node */
create(node *1)
{
printf("Enter a number and press 0 to stop:");
scanf("%d",&l->info);
if(l->info==0)
l->next=NULL;
else
{
l->next=(node *)malloc(sizeof(node));
create(l->next);
}
return 0;
}
/*Function to print the elements of linked list*/
display(node *1)
{
int count=0;
printf("\n Elements in Linked List are:\n");
while(l->info!=0)
{
Printf(“%d\t",l->info);
l=l->next;
count++; ~
}
printf("\n Number of elements in linked list:%d", count);
return 0;
}
void main()
{
clrscr();
header=(node *)malloc(sizeof(node));
create(header);
display(header);
}
OUTPUT
Enter a number and press 0 to stop:65
Enter a number and press 0 to stop:45
Enter a number and press 0 to stop:97
Enter a number and press 0 to stop:78
Enter a number and press 0 to stop:35
Enter a number and press 0 to stop:59
Enter a number and press 0 to stop:0
Elements in linked list are:
65 45 97 78 35 59
Number of elements in linked list:6