Sie sind auf Seite 1von 13

Raja Balwant Singh Management Technical Campus,

Khandari Farm,Agra

DATA STRUCTURES
Practical File
Sub code:-RCA252

Submitted by;- Submitted To:-


Vivek Kumar Sharma Dr. Krishan Kumar Goyal
Class:-MCA 2nd sem Associate Professor & Dean
Roll no:- 19000501400018 Faculty of Computer Application
Enroll no:- 190005014000935 (R.B.S Management Technical Campus, Agra)
1-Program to calculate GCD
#include<stdio.h>
#include<conio.h>
int GCD(int, int);
int main()
{
int a,b,c;
printf("Enter the two no\n");
scanf("%d%d",&a,&b);
c=GCD(a,b);
printf("\n GCD of %d and %d=%d",a,b,c);
getch();
return 0;
}
int GCD(int x, int y)
{
int d;
d=x%y;
if(d==0)
return y;
else return(GCD(y,d));
}

Output:-

Enter the two no


22 15
GCD of 22 and 15=1
2- PROGRAM FOR TOWER OF HANOI

#include<stdio.h>
#include<stdio.h>
void move(int, char, char, char);
int main()
{
int n;
printf("\nEnter the no of rings");
scanf("%d",&n);
move(n,'a','c','b');
getch();
return 0;
}
void move(int n,char a,char c, char b)
{
if(n==1)
printf("\nmove from %c to %c",a,c);
else
{
move(n-1,a,b,c);
move(1,a,c,b);
move(n-1,b,c,a);
}
}
Output:-

Enter the no of rings 3


move from a to c
move from a to b
move from c to b
move from a to c
move from b to a
move from b to c
move from a to c
3-Program to print Fibonacci series

#include<stdio.h>
#include<conio.h>
int fabo(int);
int main()
{
int n,i;
printf("Enter the term");
scanf("%d",&n);
for(i=0;i<n;i++)
printf("Fabonacci(%d)=%d\n",i,fabo(i));
getch();
return 0;
}
int fabo(int num)
{
if (num<=2)
return 1;
return (fabo(num-1)+fabo(num-2));
}

Output:-
Enter the term 8
Fabonacci(0)=1
Fabonacci(1)=1
Fabonacci(2)=1
Fabonacci(3)=2
Fabonacci(4)=3
Fabonacci(5)=5
Fabonacci(6)=8
Fabonacci(7)=13
4- PROGRAM FOR
1.PUSH 2.POP 3.PEEP 4.Display 5.CLEAR STACK
#include<stdio.h>
#include<conio.h>
#define MAX 10
int st[MAX],top=-1;
void push(int st[],int val);
int pop(int st[]);
int peep(int st[]);
void display(int st[]);
void clear_stack();
int main()
{
int val,a;
do
{
printf("\n\nmain menu");
printf("\n1. push");
printf("\n2. pop");
printf("\n3. peep");
printf("\n4.display");
printf("\n5. Clear stack");
printf("\nEnter your option");
scanf("%d",&a);
switch(a)
{
case 1:
printf("\n Enter the number to be pushed on to the stack");
scanf("%d",&val);
push(st,val);
break;
case 2:
val= pop(st);
printf("\n The value deleted from the stack is %d",val);
break;
case 3:
val= peep(st);
printf("\n The value stored at the top of the stack is %d",val);
break;
case 4:
display(st);
break;
case 5:
clear_stack();
break;

}
}while(a !=6);
getch();
return 0;
}
void push(int st[], int val)
{
if(top == MAX-1)

{
printf("\n STACK OVERRFLOW");

}
else
{
top++;
st[top]= val;

}
}
int pop(int st[])
{
int val;
if(top==-1)
{
printf("\n STACK UNDERFLOW");
return -1;
}
else
{
val=st[top];
top--;
return val;
}
}
void display (int st[])
{
int i;
if (top==-1)
printf("\n STACK IS EMPTY");
else
{
for(i=top;i>=0;i--)
printf("\n%d",st[i]);

}
}
int peep(int st[])
{
if (top==-1)
{
printf("\n STACK IS EMPTY");
return -1;

}
else
return (st[top]);

}
void clear_stack()
{
int val,i;
val=st[top];
for(i=top;i>=0;i--)
{
val=st[i];
top--;
}

printf("Stack is clear now");


}

Output:-
main menu
1. push
2. pop
3. peep
4.display
5. Clear stack
Enter your option2

The value deleted from the stack is 2


main menu
1. push
2. pop
3. peep
4.display
5. Clear stack
Enter your option5
Stack is clear now

main menu
1. push
2. pop
3. peep
4.display
5. Clear stack
Enter your option
5- PROGRAM FOR Reverse a String
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char str[100], reverse_str[100],temp;
int i,j=0;
printf("Enter the string");
gets(str);
temp=strlen(str);
for(i=temp-1;i>=0;i--)
{
reverse_str[j]=str[i];
j++;
}
reverse_str[j]='\0';
printf("\n The reversed string is %s",reverse_str);
getch();
return 0;
}

Output:-
Enter the string Raja balwant singh

The reversed string is hgnis tnawlab ajar


6- PROGRAM FOR Palindrom
#include<stdio.h>
#include<conio.h>
#include<string.h>
void checkpalin();
void chck1000( int n);
void nobt1000( int n);
void main()
{
int choose,n;
char str[100];
do
{
printf("\nMAIN MENU");
printf("\nchoose an option :-\n1.Check whether a string is PALINDROME or NOT. \n2.Which
numbers are PALINDROME between 1 to 1000.\n3.To print only PALINDROME numbers
between 1 to 1000.\n 4.EXIT\n");
printf("________________________________________________________________________
________________________________________________\n");
scanf("%d",&choose);
switch(choose)
{
case 1:
checkpalin();
break;
case 2:
for(n=1;n<=1000;n++){
printf("\n%d:-",n);
chck1000(n);
continue;
case 3:
printf("PALINDROME numbers between 1 to 1000\n");
for(n=1;n<=1000;n++)
nobt1000(n);
continue;
}}}
while(choose!=4);
getch();
}
void checkpalin( )
{
char str[100],revstr[100],test;
int i,j=0,len;
printf("enter any string\n");
scanf("%s",str);
len=strlen(str);
for(i=0;i<len;i++)
{
if(str[i]!=str[len-i-1])
{
j=1;
break;
}
}
if(j==0)
printf(" Given string is PALINDROME");
else
printf("Given string is not PALINDROME");
}
void chck1000( int n)
{int r=0,t;
t=n;
while(t!=0)
{
r=r*10;
r=r+t%10;
t=t/10;
}
if(n==r)
printf("YES!! palindrome");
else
printf("NO** not palindrome"); }
void nobt1000( int n)
{int r=0,t,x=0;
t=n;
while(t!=0)
{
r=r*10;
r=r+t%10;
t=t/10;
x++;
}
if(n==r)
printf("%d\t",n);
printf("total number of palindrome numbers between 1 to 1000 is %d",x);
}

OUTPUT:-

MAIN MENU

choose an option :-

1.Check whether a string is PALINDROME or NOT.

2.Which numbers are PALINDROME between 1 to 1000.

3.To print only PALINDROME numbers between 1 to 1000.

4.EXIT
7- PROGRAM FOR Print Factorial
#include<stdio.h>
#include<conio.h>
int fact(int);
int main()
{
int n,b;
printf("Enter any no");
scanf("%d",&n);
b=fact(n);
printf("factorial of %d=%d",n,b);
}
int fact(a)
{
int i,c;
for(i=1;i<a;i++)
{
c=a*fact(a-1);
return (c);
}

OUTPUT:-
Enter any no6
factorial of 6=720

Das könnte Ihnen auch gefallen