0% found this document useful (0 votes)
69 views3 pages

Write C++ Program That Find Factorial Using Recursive Function

The document contains C++ code examples for three recursive functions: 1) A recursive function to calculate the factorial of a number. 2) A recursive function to calculate power using a for loop. 3) A recursive function to calculate the Fibonacci number.

Uploaded by

tarek mahmoud
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views3 pages

Write C++ Program That Find Factorial Using Recursive Function

The document contains C++ code examples for three recursive functions: 1) A recursive function to calculate the factorial of a number. 2) A recursive function to calculate power using a for loop. 3) A recursive function to calculate the Fibonacci number.

Uploaded by

tarek mahmoud
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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