C++ Program to Find Factorial

C++ program to find factorial is provided on this page.

The aim of this program is to find the factorial of any number.

Before that lets see what factorial is.

Factorial is the product of all positive integers less than or equal to N, where N is the number for which we are calculating factorial.

Thigns to remember.

First, according to this definition you cannot calculate factorial for negative integers.

Secondly, you cannot calculate factorial for non-integers.

Third thing to remember, factorial of 0 is 1.

Factorial of a number n is denoted by n!

C++ program to find factorial

In this program, we are going to ask the user for a positive integer n and then program will calculate the factorial of that number.

For example, factorial of 4 and 6 are caulculated as given below.

4! = 4*3*2*1 = 24
6! = 6*5*4*3*2*1 = 720	

Here is the c++ program to calculate factorial of a number.

#include <iostream>
using namespace std;

int main()
{
    int number;
    long long factorial = 1;

    cout << "Enter any positive number: ";
    cin >> n;

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

    cout << "Factorial of " << number << " = " << factorial;    
    return 0;
}

Output:

Enter any positive number: 7
Factorial of 7 = 5040

Comments