int algorithm ( int n ) {
if (n == 0)
return 1;
else {
return 2 * algorithm(n-1);
}
}
#include <iostream>
using namespace std;
// This algorithm is simply calculating the 2^n (2 raise to
power n)
int algorithm ( int n ) {
// Recursion will stop when n becomes 1
if (n == 0)
return 1;
else {
// 2 will be mulitplied with result in each call and value of n wil
be decremented by 1
return 2 * algorithm(n-1);
}
}
int main()
{
int n;
// Input of n
cout<<"Enter the value of n: ";
cin>>n;
cout<<" Algorithm will result "<<algorithm(n);
return 0;
}
OUTPUT:
Get Answers For Free
Most questions answered within 1 hours.