write a recursive function called m(i) to compute the following series m(i)= 1 + 1/2 +1/3+....+1/i
Demonstrate the function by calling it in a loop that displays m(i) for i = 1,2,3,.....,10
Program for the language c++
Answers
Answered by
5
The given problem is solved using language - C++.
#include <iostream>
using namespace std;
double m(int n){
if(n==1)
return 1.0;
return 1/(double)n + m(n-1);
}
int main() {
cout<<"Enter limit - ";
int n;
cin>>n;
cout<<"Sum is: "<<m(n);
return 0;
}
Here we have created a recursive function m() which calculates the sum of the series. Recursive function is a function which calls itself. It must have a base case which is used to terminate the recursion.
In mathematical representation, we can write the function as,
Hence, the problem is solved.
See the attachment for output.
Attachments:
Similar questions
Math,
2 months ago
India Languages,
2 months ago
Accountancy,
2 months ago
Math,
5 months ago
Math,
10 months ago