Write a C++ program to find the sum of factorial of a given number using recursive function.
Implementation of the above problem:
#include<iostream.h>
int factorial(int n)
{
int fact;
if(n==1 || n==0)
{
return 1;
}
fact=n*factorial(n-1); //recursive function
return fact;
}
int main()
{
int n,i,sum=0;
cout<<"\nEnter the number of terms\n";
cin >>n;
for(i=1;i<=n;i++)
{
sum+=factorial(i);
}
cout<<"Sum of factorial of first "<<n<<" number is "<<sum;
return 0;
}
Thanks
Mukesh Rajput
Post A Comment:
0 comments: