Recursion:
1.
Write a program to find the factorial of number
using recursive method.
Source code:
#include<stdio.h>
#include<conio.h>
int factorial(int n)
{
if(n==0||n==1)
return 1;
else
return
n*factorial(n-1);
}
void main()
{ int n,x;
printf("Enter the number:");
scanf("%d",&n);
x=factorial(n);
printf("The factorial of
%d=%d",n,x);
getch();
}
2.
Write a program to multiply
two numbers by recursive method.
Source code:
#include<stdio.h>
#include<conio.h>
int multiplication(int a,int b)
{
if(b==1)
return a;
else
return
a+multiplication(a,b-1);
}
void main()
{ int a,b,x;
clrscr();
printf("Enter the first number to multiply::");
scanf("%d",&a);
printf("Enter the second number to multiply::");
scanf("%d",&b);
x=multiplication(a,b);
printf("%d*%d=%d",a,b,x);
getch();
}
3.
Write a program to find the number one exponent
to other(ab) using recursion
Source code:
#include<stdio.h>
#include<conio.h>
int power(int a,int b)
{ if(b==0)
return 1;
else
return
a*power(a,b-1);
}
void main()
{ int a,b,x;
printf("Enter the number a:") ;
scanf("%d",&a);
printf("Enter the number b:");
scanf("%d",&b);
x=power(a,b);
printf("%d
power %d=%d",a,b,x);
getch();
}
4.
Write a program to slove the tower of Hanoi
problem using recursion.
Source code:
#include<stdio.h>
#include<conio.h>
void movedisk(int n,char s,char t,char a)
{
if(n==1)
{
printf("\n move disk from %c to %c",s,t);
return;
}
movedisk(n-1,s,a,t);
printf("\n move disk from %c to %c",s,t);
movedisk(n-1,a,t,s);
}
main()
{
int n;
clrscr();
printf("How many disk\n:");
scanf("%d",&n);
movedisk(n,'s','t','a');
getch();
return 0;
}