# C++ Program to find Factorial of a Number

Last Updated:

## What is Factorial of a Number?

The factorial of a number n can be defined as the product of all positive numbers less than or equal to n. For example, the factorial of 6 is 1 × 2 × 3 × 4 × 5 × 6 = 720.

## Examples

The following table provides few examples of factorial of a number.

NumberFactorialResult
1! 1 1
2! 1 × 2 2
3! 1 × 2 × 3 6
4! 1 × 2 × 3 × 4 24

Note: In Mathematics, factorial of a number is represented by exclamation mark.

Tips: It is recommended to use our online Factorial of a Number calculator for better understanding.

## Find a Factorial of a Number

In the following example, we will find a factorial of a number 6.

### Example

C++ Compiler
#include <iostream> using namespace std; int main() { int num = 6; int i = 1; int factorial = 1; for(i=1; i<=num; i++) factorial *= i; if(num > 0) cout << "Factorial of " << num <<": " << factorial; else cout << "Factorial of 0: 0\n"; return 0; }

## Find a Factorial of any Given Number

In the following example, we will find a factorial of any given number.

### Example

C++ Compiler
#include <iostream> using namespace std; int main() { int num; int i = 1; int factorial = 1; cout << "Enter a (int) number: "; cin >> num; for(i=1; i<=num; i++) factorial *= i; if(num > 0) cout << "Factorial of " << num <<": " << factorial; else cout << "Factorial of 0: 0\n"; return 0; }