Quote:
Originally Posted by bitarno
2nd Exercise
Consider the following series
x(n) = 1/n + 1/(n-1) + … + 1 for all n > 0
x(1) = 1
Develop a recursive function to compute the value of x(n)
|
Code:
#include <iotream>
using namespace std;
double function(int);
int main(void)
{
int n;
double x;
do{
cout << "Enter n: ";
cin >> n;
}while(n<1);
x = function(n);
cout << "X(n) = " << x << endl;
return 0;
}
double function(int n)
{
if(n == 1)
return 1;
return 1/n + function(n-1);
}
Damn I Miss The C++