Vipin | Tue, 02 Jun, 2020 | 151

In this example, you will learn to calculate the factorial of a number entered by the user.

The factorial of a positive number `n` is given by:

```
factorial of n (n!) = 1 * 2 * 3 * 4....n
```

The factorial of a negative number doesn't exist. And, the factorial of 0 is 1.

```
#include <iostream>
using namespace std;
int main() {
int n, i;
unsigned long long fact = 1;
cout<<"Enter an integer: ";
cin>>n;
// shows error if the user enters a negative integer
if (n < 0)
cout<<"Error! Factorial of a negative number doesn't exist.";
else {
for (i = 1; i <= n; ++i) {
fact *= i;
}
cout<<"Factorial of "<< n<<"is"<<fact;
}
return 0;
}
```

**Output**

Enter an integer: 10 Factorial of 10 = 3628800