Factorial of a Number
Program to calculate factorial of a number
C++ Factorial of a Number Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
using namespace std;
int main() {
int n;
long long factorial = 1;
cout << "Enter a positive integer: ";
cin >> n;
if (n < 0) {
cout << "Factorial is not defined for negative numbers" << endl;
} else {
for (int i = 1; i <= n; i++) {
factorial *= i;
}
cout << "Factorial of " << n << " = " << factorial << endl;
}
return 0;
}Enter a positive integer: 5 Factorial of 5 = 120
Understanding Factorial of a Number
This program calculates the factorial of a number. Factorial is one of the most fundamental concepts in mathematics and programming, used extensively in permutations, combinations, probability, and many algorithms. The program demonstrates iterative multiplication, data type selection for large numbers, and input validation.
---
1. What is Factorial?
Factorial of a non-negative integer n (denoted as n!) is the product of all positive integers from 1 to n.
Mathematical definition:
Examples:
120
24
6
2
1
1
Applications:
---
2. Header File: #include <iostream>
#include <iostream>
Provides:
cout → for displaying outputcin → for reading input---
3. Declaring Variables
int n;
long long factorial = 1;
Variable `n`:
Variable `factorial`:
-
Initialized to 1
Why initialize to 1?
Why use `long long` instead of `int`?
int can store values up to ~2.1 billionlong long can store up to ~9.2 × 10^18---
4. Taking Input From User
cin >> n;
nExample:
5
n = 5---
5. Input Validation - Checking for Negative Numbers
if (n < 0)
cout << "Factorial is not defined for negative numbers" << endl;
Why check for negative?
What happens:
---
6. Calculating Factorial Using For Loop
for (int i = 1; i <= n; i++)
factorial *= i;
How the loop works:
For n = 5:
Iteration 1 (i = 1):
factorial = factorial * i → factorial = 1 * 1 = 1factorial = 1Iteration 2 (i = 2):
factorial = 1 * 2 = 2factorial = 2Iteration 3 (i = 3):
factorial = 2 * 3 = 6factorial = 6Iteration 4 (i = 4):
factorial = 6 * 4 = 24factorial = 24Iteration 5 (i = 5):
factorial = 24 * 5 = 120factorial = 120After loop:
factorial = 120 ✅
---
7. Understanding the Accumulation Pattern
factorial *= i; is shorthand for factorial = factorial * i;
This is the
multiplicative accumulation pattern
:
Visual representation:
---
8. Special Case: Factorial of 0
What if user enters 0?
i <= 0 → 1 <= 0 isfalse
factorial remains 1This is correct because
0! = 1
by mathematical definition.
Why is 0! = 1?
---
9. Displaying the Result
cout << "Factorial of " << n << " = " << factorial << endl;
This prints:
n: 5factorial: 120Output:
Factorial of 5 = 120
---
10. Why Factorials Grow So Fast
Factorials increase extremely rapidly:
This is why we use `long long`:
int can only store up to ~2.1 billionlong long can handle up to 20! safelyFor larger factorials:
---
11. Alternative: Recursive Approach
Factorial can also be calculated recursively:
int factorial(int n) {
if (n <= 1) return 1;
}
return n * factorial(n - 1);Iterative (our approach):
Recursive:
---
12. Real-World Applications
Permutations:
Combinations:
Series:
---
Summary
long long to handle large factorialsThis program teaches:
Mastering factorial helps in:
Factorial is one of the most fundamental concepts in mathematics and programming, and understanding it deeply helps in solving many advanced problems.
Let us now understand every line and the components of the above program.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.
Practical Learning Notes for Factorial of a Number
This C++ program is part of the "Loop Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.