by CodeChum Admin
For those of you who may not now, a factorial is a product of multiplying from 1 until the number. Now, you must make a program that will accept an integer and then print out its factorial using loops.
Are you up for this task?
Instructions:
Input
A line containing an integer.
5
Output
A line containing an integer.
120
#include <iostream>
using namespace std;
int main() {
int n;
long double factorial = 1.0;
cin >> n;
if (n < 0)
cout << "Error! Factorial of a negative number doesn't exist.";
else {
for(int i = 1; i <= n; ++i) {
factorial *= i;
}
cout << factorial;
}
cin>>n;
return 0;
}
Comments
Leave a comment