You are on page 1of 3

1. Write C++ Program that find factorial using recursive function .

#include <iostream.h>

int factorial(int n)

if (n == 0) return 1;

return n * factorial(n-1);

main()

int n;

cout << "Enter a non-negative integer: ";

cin >> n;
cout << "Factorial of " << n << " is " << factorial(n) <<
endl;

return 0;

}
2. Write C++ Program that compute power using recursive function .

#include <iostream.h>
int power (int base,int e)
{

int p = 1;

for (int i = 0; i < e; i++)


{

p*=base;
}

return p;
}
int main()
{

int b, e,pow;

cout << "Enter base number: ";


cin>>b;
cout << "Enter exponention number: ";
cin>>e;

pow=power(b,e);

cout << "power ( " <<b<<”,”<<e<<”)=”<< pow << endl;

return 0;
}
3. Write C++ Program that fined Fibonacci number using recursive function.
Fib(n)=fib(n-1) + fib(n-2)
Where fib(0)=1
Fib(1)=1
#include <iostream.h>

int fib(int n)
{
if (n == 0 || n == 1) return 1;
return fib(n-1) + fib(n-2);
}

main()
{
int n;

cout << "Enter a non-negative integer: ";


cin >> n;
cout << "Fib(" << n << ") = " << fib(n) << "." << endl;
return 0;
}

You might also like