Easy Tutorial
❮ Cpp Tutorial Cpp Intro ❯

C++ Example - Calculate the Factorial of a Number

C++ Examples

The factorial of a positive integer (English: factorial) is the product of all positive integers less than or equal to that number, and the factorial of 0 is 1. The factorial of a natural number n is written as n!.

Example

#include <iostream>
using namespace std;

int main()
{
    unsigned int n;
    unsigned long long factorial = 1;

    cout << "Enter an integer: ";
    cin >> n;

    for(int i = 1; i <= n; ++i)
    {
        factorial *= i;
    }

    cout << n << " factorial is: " << " = " << factorial;
    return 0;
}

The output of the above program is:

Enter an integer: 12
12 factorial is: = 479001600

C++ Examples

❮ Cpp Tutorial Cpp Intro ❯