# C++ Example – C++ Program to Find Factorial

What is Factorial?

The Factorial of a specified number refers to the product of a given series of consecutive whole numbers beginning with 1 and ending with the specified number
We use the “!” to represent factorial
Eg.
5! = 1 x 2 x 3 x 4 x 5 = 120

It’s A Fact!
The number of ways of arranging n objects is n!
n! = n x (n − 1) x (n − 2) x . . . x 3 x 2 x 1

### How do you implement the factorial function in C++

```#include <iostream>
using namespace std;
int fact (int i) {
int result = 1;
while (i > 0) {
result = result * i;
i = i-1;
}
return(result);
}

int main () {
int n;
cout << "Enter a natural number: ";
cin >> n;
while (n < 0) {
cin >> n;
}
cout << n << "! = " << fact(n) << endl;
return(0);
}
/*
OUTPUT:
Enter a natural number: 5
5! = 120
*/
```

C++ Program to Find Factorial

```#include <iostream>
using namespace std;

int main () {
unsigned int n;
unsigned long long factorial = 1;
cout << "Enter a positive integer: ";

cin >> n;
for(int i = 1; i <=n; ++i) {
factorial *= i;
}
cout << "Factorial of " << n << " = " << factorial;
return(0);
}
/*
OUTPUT:
Enter a positive integer: 5
Factorial of 5 = 120
*/
```