//Program to write recursive function for
//a)Tower of hanoi
//b)power of x to n
//c)Fibonacci series
#include <stdio.h>
void TOH(int n, char A, char B , char C)
{
if(n>0)
{
TOH(n-1,A,C,B);
printf("\n%c->%c",A,B);
TOH(n-1,C,B,A);
}
}
int fib(int n)
{
if(n==0)
return(0);
if(n==1)
return(1);
return(fib(n-1)+fib(n-2));
}
int power(int x , int n)
{
if(n==0)
return(1);
return(x*power(x,n-1));
}
int main()
{
int n,x,op,i;
do
{
printf("\n1)Tower of Hanoi\n2)Power of x to the n");
printf("\n3)Fibonacci series\n4)Quit");
printf("\nEnter Your choice : ");
scanf("%d",&op);
switch(op)
{
case 1: printf("\nEnter No. of plates : ");
scanf("%d",&n);
TOH(n,'A','B','C');
break;
case 2: printf("\nEnter x and n : ");
scanf("%d%d",&x,&n);
printf("%d",power(x,n));
break;
case 3: printf("\nEnter No. of Terms: ");
scanf("%d",&n);
for(i=0;i<n;i++)
printf("%d ",fib(i));
break;
}
}while(op!=4);
}
No comments:
Post a Comment