Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Factorial of a Number in C++

Compute the factorial of a number in C++ with a for loop and a long long result. Covers 0! = 1, rejecting negatives, and where even 64 bits overflow (past 20!).

BeginnerLoop ProgramsExample 3 of 14
factorial-of-a-number.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 int n;
6 long long factorial = 1;
7
8 cout << "Enter a positive integer: ";
9 cin >> n;
10
11 if (n < 0) {
12 cout << "Factorial is not defined for negative numbers" << endl;
13 } else {
14 for (int i = 1; i <= n; i++) {
15 factorial *= i;
16 }
17 cout << "Factorial of " << n << " = " << factorial << endl;
18 }
19
20 return 0;
21}

Sample Output

Factorial of 5 = 120

1 × 2 × 3 × 4 × 5.

In this one

  • How to multiply in a loop with an accumulator that starts at 1
  • Why 0! is 1
  • Why the result uses long long, not int
  • Where long long overflows

How it works

  1. 1

    Read n (5).

  2. 2

    If n < 0, print an error and stop.

  3. 3

    factorial starts at 1.

  4. 4

    Multiply by 1, 2, 3, 4, 5 in turn.

  5. 5

    Print 120.

Edge cases

Large n

Enter 25.

long long holds up to about 9.2 × 10^18, so it is exact up to 20!. 21! overflows and prints a wrong value. A 32-bit int would already overflow after 12!.

Zero

Enter 0.

The loop does not run, so factorial stays 1, which is the correct value of 0!.

What's going on

n! in C++ is 1 × 2 × … × n. 5! is 120. That's the sample. 0! is 1. Negative n is not defined.

int n;

long long factorial = 1;

Start the product at 1, not 0. 0 * anything is 0, and you never recover.

Why long long: 13! is 6,227,020,800. That does not fit in a 32-bit int (~2.1 billion). long long holds through 20!. 21! overflows even that.

cin >> n;

Sample: 5.

if (n < 0)

Print that factorial isn't defined. Don't run the loop on -3.

for (int i = 1; i <= n; i++)

factorial *= i; (same as factorial = factorial * i)

Walk 5:

start 1
×1 → 1
×2 → 2
×3 → 6
×4 → 24
×5 → 120

n == 0: the loop condition 1 <= 0 is false. factorial stays 1. That's 0!.

n == 1: one iteration, still 1.

Then cout << "Factorial of " << n << " = " << factorial. Sample: Factorial of 5 = 120.

A recursive fact(n) = n * fact(n-1) is the same math with a stack. Missing the n <= 1 base case is a crash, not a clever answer.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What is the largest n whose factorial fits in a 32-bit signed int?

Pick an answer to check it.

Question 2

What does this program print for n = 0?

Pick an answer to check it.

Questions people ask

Why does factorial start at 1, not 0?

Multiplying anything by 0 gives 0. 1 is the neutral starting point for a product.

Is there a recursive version?

Yes, see Factorial using Recursion. It needs a base case or it calls itself forever.

How do I get exact factorials above 20?

You need a big-number library or store digits in an array or vector and multiply by hand.