Answer to Question #152518 in C++ for samuel

Question #152518

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.


1
Expert's answer
2020-12-22T10:05:10-0500
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog