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; }
Comments
Leave a comment