I have a problem to solve in C++ which is as follow : 1 + (1/2!) + (1/3!) + (1/4!) + (1/5!)... and so on
The code I have written for the same is as follows
#include <iostream>
using namespace std;
int fact(int);
int main()
{
int n; float sum=0;
cout<<"Enter number of terms:";
cin>>n;
for(int i=1;i<=n;i++)
{
sum = sum + (float)(1/(fact(i)));
}
cout<<endl<<"The sum is :"<<sum;
return 0;
}
int fact(int x)
{
if(x == 0 || x == 1)
return 1;
else
return x * fact(x-1);
}
The above code is not returning any output. Earlier I have computed factorial without using for loop. And the recursion has worked.