0
0
CppProgramBeginner · 2 min read

C++ Program to Find Factorial of a Number

You can find the factorial of a number in C++ by using a loop that multiplies numbers from 1 to the given number, like for (int i = 1; i <= n; ++i) result *= i;.
📋

Examples

Input0
OutputFactorial of 0 is 1
Input5
OutputFactorial of 5 is 120
Input10
OutputFactorial of 10 is 3628800
🧠

How to Think About It

To find the factorial of a number, multiply all whole numbers from 1 up to that number. If the number is 0, the factorial is 1 by definition. Use a loop to multiply these numbers step by step.
📐

Algorithm

1
Get the input number from the user
2
If the number is 0, return 1 as factorial
3
Initialize a variable to 1 to store the result
4
Loop from 1 to the input number
5
Multiply the result by the current loop number each time
6
After the loop ends, return the result
💻

Code

cpp
#include <iostream>
using namespace std;

int main() {
    int n;
    unsigned long long factorial = 1;
    cout << "Enter a positive integer: ";
    cin >> n;
    if (n < 0) {
        cout << "Factorial is not defined for negative numbers." << endl;
        return 1;
    }
    for (int i = 1; i <= n; ++i) {
        factorial *= i;
    }
    cout << "Factorial of " << n << " is " << factorial << endl;
    return 0;
}
Output
Enter a positive integer: 5 Factorial of 5 is 120
🔍

Dry Run

Let's trace the input 5 through the code

1

Input

User enters n = 5

2

Initialize factorial

factorial = 1

3

Loop iteration 1

i = 1, factorial = 1 * 1 = 1

4

Loop iteration 2

i = 2, factorial = 1 * 2 = 2

5

Loop iteration 3

i = 3, factorial = 2 * 3 = 6

6

Loop iteration 4

i = 4, factorial = 6 * 4 = 24

7

Loop iteration 5

i = 5, factorial = 24 * 5 = 120

8

End loop

factorial = 120

ifactorial
11
22
36
424
5120
💡

Why This Works

Step 1: Initialize factorial

We start with factorial = 1 because multiplying by 1 does not change the result and it is the identity for multiplication.

Step 2: Multiply in loop

Each loop step multiplies factorial by the current number i, building the product of all numbers from 1 to n.

Step 3: Final result

After the loop finishes, factorial holds the product of all numbers from 1 to n, which is the factorial.

🔄

Alternative Approaches

Recursive function
cpp
#include <iostream>
using namespace std;

unsigned long long factorial(int n) {
    if (n <= 1) return 1;
    return n * factorial(n - 1);
}

int main() {
    int n;
    cout << "Enter a positive integer: ";
    cin >> n;
    if (n < 0) {
        cout << "Factorial is not defined for negative numbers." << endl;
        return 1;
    }
    cout << "Factorial of " << n << " is " << factorial(n) << endl;
    return 0;
}
Uses recursion which is elegant but can cause stack overflow for very large n.
Using while loop
cpp
#include <iostream>
using namespace std;

int main() {
    int n;
    unsigned long long factorial = 1;
    cout << "Enter a positive integer: ";
    cin >> n;
    if (n < 0) {
        cout << "Factorial is not defined for negative numbers." << endl;
        return 1;
    }
    int i = 1;
    while (i <= n) {
        factorial *= i;
        i++;
    }
    cout << "Factorial of " << n << " is " << factorial << endl;
    return 0;
}
Uses a while loop instead of for loop, functionally the same but different style.

Complexity: O(n) time, O(1) space

Time Complexity

The program multiplies numbers from 1 to n once, so it runs in linear time O(n).

Space Complexity

Only a few variables are used, so space is constant O(1).

Which Approach is Fastest?

The iterative for loop is fastest and safest for large n compared to recursion which can cause stack overflow.

ApproachTimeSpaceBest For
Iterative for loopO(n)O(1)Large inputs, safe and fast
Recursive functionO(n)O(n)Simple code, small inputs
While loopO(n)O(1)Same as for loop, style preference
💡
Use unsigned long long to store factorials of larger numbers safely.
⚠️
Forgetting that factorial of 0 is 1 and returning 0 instead.