Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

PART A :

DSA LAB PROGAM 1:


Program to find GCD using recursive function.

#include<stdio.h>
#include<conio.h>
int gcd(int i, int j);
void main()
{
int a,b;
printf("Enter two integer numbers\n");
scanf("%d%d",&a,&b);
printf("GCD of %d and %d is %d\n",a,b,gcd(a,b));
getch();
}
int gcd(int i, int j)
{
if(j>i)
return gcd(j,i);
if(j==0)
return i;
else
return gcd(j,i%j);
}
PART A :
DSA LAB PROGAM 2:

Program to generate binomial coefficient using


recursive Function.

#include <stdio.h>
#include<conio.h>
int fact(int);
void main()
{
int n,r,ncr;
printf("Enter N and R values: \n");
scanf("%d %d",&n,&r);
ncr=fact(n)/(fact(r)*fact(n-r));
printf("The NCR of %d and %d is %d",n,r,ncr);
getch();
}
int fact(int x)
{
int i=1;
while(x!=0)
{
i=i*x;
x--;
}
return i;
}
PART A :
DSA LAB PROGAM 3:
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
void move_disks(int,char,char,char);
clrscr();
printf("Enter the number of disks\n");
scanf("%d",&n);
move_disks(n,'s','d','t');
getch();
}
void move_disks(int n,char source,char destin,char temp)
{
if(n>0)
{
move_disks(n-1,source,temp,destin);
printf("Move disk %d from %c to
%c\n",n,source,destin);
move_disks(n-1,temp,destin,source);
}
return;
}
PART A :
DSA LAB PROGAM 4:
Program to read the names of cities and arrange them alphabetically.

#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
int i,j,n;
char str[50][50],s[50];
printf("Enter number of names\n");
scanf("%d",&n);
printf("Enter cities in any order:\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("The sorted order of names are:\n");
for(i=0;i<n;i++)
{
printf("%s\n",str[i]);
}
getch();
}
PART A :
DSA LAB PROGAM 5:
Program to sort the given list using selection sort
Technique.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[50],n,i,j,pos,swap;
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d numbers\n",n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
for(i = 0; i < n - 1; i++)
{
pos=i;
for(j = i + 1; j < n; j++)
{
if(a[pos] > a[j])
pos=j;
}
if(pos!= i)
{
swap=a[i];
a[i]=a[pos];
a[pos]=swap;
}
}
printf("Sorted Array\n");
for(i = 0; i < n; i++)
printf("%d\t", a[i]);
getch();
}
PART A :
DSA LAB PROGAM 6:
Program to sort the given list using bubble sort Technique.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[50],n,i,j,temp;
printf("Enter the Number of Elements\n");
scanf("%d",&n);
printf("Enter the %d numbers:\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp = a[j];
a[j]=a[j + 1];
a[j+1]=temp;
}
}
}
printf("After sorting\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}
PART A :
DSA LAB PROGAM 7:
Program to sort the given list using insertion sort technique
#include<stdio.h>
#include<conio.h>
void main()
{

int i,j,n,temp,num[25];
printf("Enter the number of elements\n");
scanf("%d",&n);
printf("Enter %d elements:",n);
for(i=0;i<n;i++)
scanf("%d",&num[i]);
for(i=1;i<n;i++)
{
temp=num[i];
j=i-1;
while((temp<num[j])&&(j>=0))
{
num[j+1]=num[j];
j=j-1;
}
num[j+1]=temp;
}
printf("Sorted Elements are\n");
for(i=0;i<n;i++)
printf(" %d",num[i]);
getch();
}
PART A :
DSA LAB PROGAM 8:
Program to implement the operations of simple stack.
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define STACK_SIZE 5
int top=-1,Stack[STACK_SIZE],choice;
void main()
{
void PUSH(void);
void POP(void);
void display(void);
clrscr();
while(1)
{
printf("STACK OPERATIONs\n");
printf("1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\n");
printf("Enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:PUSH();
break;
case 2:POP();
break;
case 3:display();
break;
case 4:exit(0);
break;
default:printf("invalid choice\n");
break;
}
}
}
void PUSH()
{
int element;
if(top==(STACK_SIZE-1))
{
printf("Stack is full or overflow\n");
return;
}
else
{
printf("Enter the element to be inserted\n");
scanf("%d",&element);
top=top+1;
Stack[top]=element;
}}
void POP()
{
int element_deleted;
if(top==-1)
{
printf("Stack is empty or underflow\n");
return;
}
else
{
element_deleted=Stack[top];
printf("Element deleted is %d\n",element_deleted);
top=top-1;
}
}
void display()
{
int i;
if(top==-1)
{
printf("Stack is empty or underflow\n");
return;
}
else
{
printf("Content of the Stack is\n");
for(i=top;i>=0;i--)
{
printf("%d\n",Stack[i]);
}
}
}
PART A :
DSA LAB PROGAM 9:
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define Q_size 5
int front=-1, rear=-1,choice,Q[Q_size];
void main()
{
void Qinsert(void);
void Qdelete(void);
void Qdisplay(void);
clrscr();
while(1)
{
printf("QUEUE OPERATIONS\n");
printf("1.Qinsert\n2.Qdelete\n3.Qdisplay\n4.Exit\n");
printf("Enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1: Qinsert();
break;
case 2: Qdelete();
break;
case 3: Qdisplay();
break;
case 4: exit(0);
break;
default :printf("Invalid choice\n");
break;
}
}
}
void Qinsert()
{
int element;
if(rear==(Q_size-1))
{
printf("Queue is full\n");
return;
}
else
{
printf("Enter the element to be inserted\n");
scanf("%d",&element);
rear=rear+1;
Q[rear]=element;
if(front==-1)
ront=front+1;
}}
void Qdelete()
{
int element_deleted;
if(front==-1)
{
printf("Queue is empty\n");
return;
}
else
{
element_deleted=Q[front];
printf("Element deleted is %d\n",element_deleted);
if(front==rear)
{
front=rear=-1;
}
else
{
front++;
}
}
}
void Qdisplay()
{
int i;
if(front==-1)
{
printf("Queue is empty\n");
return;
}
else
{
printf("Content of the is Queue is\n");
for(i=front;i<=rear;i++)
{
printf("%d\n",Q[i]);
}
}
}

You might also like