Create a C++ program that will use a recursive function to calculate the value o
ID: 3626660 • Letter: C
Question
Create a C++ program that will use a recursive function to calculate the value of the following expression1/1 + 1/2 + 1/3 + 1/4 + ... + 1/n
where n is a positive integer supplied by the user.
The program should prompt the user for a positive integer, then use the function to compute the sum. For example, if the user enters 3, then the expression will be 1/1 + 1/2 + 1/3 and the sum is 1.83333
Start with the template program.
A sample session...
Please enter a positive integer: 3
The sum is: 1.83333
What to hand in:
A C++ program named a5q1.cpp containing the C++ program.
Explanation / Answer
please rate - thanks
#include <iostream >
using namespace std;
double series(int);
int main()
{int number;
cout<<"Enter a positive integer ";
cin>>number;
while(number<=0)
{cout<<"Number must be > 0! ";
cout<<"Enter a positive integer ";
cin>>number;
}
cout<<"The sum is: "<<series(number)<<endl;
system("pause");
return 0;
}
double series(int n)
{if(n==1)
return 1;
else
return 1./(double)n +series(n-1);
}