Write a program that enter a number from user and display its factorial. e.g. factorial of 5 is
1 X 2 X 3 X 4 X 5=120. Where is N<=10. by using do while loop
#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