Write a C++ code that computes the sum of the following series. Sum = 1! + 2! + 3! + 4! + …n! The program should accept the number from the user.
#include <iostream>
int fact(unsigned long number){
if(number == 1)
return 1;
return fact(number -1)*number;
}
int main()
{
int N;
std::cout<<"Enter N: ";
std::cin>>N;
if(N<1){
std::cout<<"Incorrect data."<<std::endl;
system("pause");
return 1;
}
long sum=0;
for(int i=1;i<=N;i++)
sum+=fact(i);
system("pause");
std::cout<<"Sum :"<<sum<<std::endl;
return 0;
}
Comments
Leave a comment