C++ Program to Find Factorial

0

For any positive number n, it’s factorial is given by:

factorial = 1*2*3...*n

Factorial of negative number cannot be found and factorial of 0 is 1.

Program

#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: 12
Factorial of 12 = 479001600

Leave a Reply

Your email address will not be published. Required fields are marked *