Answer to Question #24620 in C++ for Calvin Thames

Question #24620
Write a program that asks user to input a number N. The program will check if N is between 1 and 10. If the number is not in the proper interval, output “Incorrect interval”. Then the program will calculate and output the sum of factorials: 1+2!+3!+...+N!
1
Expert's answer
2013-02-19T09:40:43-0500
#include <iostream>

#include <conio.h>

using namespace std;

long factorial(int n)
{
long result = 1;
while (n)
result *= n--;
return result;
}

int main()
{
int N;
cout << "Enter a number between 1 and 10: ";
cin >> N;

if (N < 0 || N > 10)
cout << "Incorrect interval." << endl;
else
{
long sum = 1;
for (int k = 2; k <= N; k++)
sum += factorial(k);
cout << endl << "The sum of factorials is: " << sum << endl;
}

_getch();
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
APPROVED BY CLIENTS