Professional Documents
Culture Documents
Management
24/1A, Chandi Ghosh Road, Tollygunge, Kolkata-
700040
ASSIGNMENT: IV
4 Write a recursion function to find out x to the power n where x and n are two given
numbers.
5 Write a recursion function to implement Tower of Hanoi problem of n disks.
6. Write a recursion function to find out combination nCk
Program:- 1
Write a tail recursion function to find out the Factorial of a given number.
Code-
#include<stdio.h>
int factorial( int n, int fact )
{
if ( n==1 )
return fact;
else
factorial( n-1, n*fact );
}
if ( n < 0 )
printf( "No factorial of negative number\n" );
else if ( n==0 )
printf( "Factorial of zero is 1\n" );
else
{
value = factorial( n,1 );
printf( "Factorial of %d = %d\n",n,value );
}
return 0;
}
Output-
Program-2
Write a recursion function to display Fibonacci series.
Code-
#include <stdio.h>
int n,count,t1,t2,t3;
int fibo (int a, int b);
int main()
{
printf ("\n How many no of terms to be printed?");
scanf("%d",&n);
t1=0;
t2=1;
printf("\n The first %d term in Fibonacci in Fibonacci series are \n",n);
printf(" %d %d ",t1,t2);
count=2;
fibo(t1,t2);
}
int fibo(int t1,int t2)
{
if(count>=n)
return 0;
else
{
t3=t1+t2;
printf("%5d",t3);
count++;
t1=t2;
t2=t3;
fibo(t1,t2);
}
}
Output-
Program-3
Write a recursion function to find out the GCD of two given numbers.
Code-
#include <stdio.h>
int hcf(int n1, int n2);
int main() {
int n1, n2;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
printf("G.C.D of %d and %d is %d.", n1, n2, hcf(n1, n2));
return 0;
}
int main()
{
int num;
}
int main()
{
int n,r;
}
Output-
-----------------------------------------x------------------------------------------