# 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!

Expert's answer

#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;

}

#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;

}

## Comments

## Leave a comment