write a program that enter number from user and display its factorial.e.g. factorial of 5 is 1×2×3×4×5=120. Where N<=10
#include <iostream>
using namespace std;
int main() {
int n;
long int factorial = 1;
cout << "Enter integer N (1-10): ";
cin >> n;
if (n < 0)
cout << "Error! Factorial of a negative number doesn't exist";
else if (n > 10)
cout << "Error! N must be <=10";
else {
for(int i = 1; i <= n; ++i) {
factorial *= i;
}
cout << "Factorial of " << n << " = " << factorial;
}
return 0;
}
Comments
Leave a comment