What is Factorial?
The factorial of a non-negative integer n, written as n!, is the product of all positive integers from 1 to n.
Formula: n! = n × (n − 1) × (n − 2) × ... × 2 × 1
For example:
- 5! = 5 × 4 × 3 × 2 × 1 = 120
- 4! = 4 × 3 × 2 × 1 = 24
- 3! = 3 × 2 × 1 = 6
- 0! = 1 (by definition)
In this tutorial, we'll write a C program to find the factorial of a number using a while loop. The while loop is a good choice here because it checks the condition before each iteration.
Factorial Program in C Using While Loop
Here is the complete C program that reads a number from the user and calculates its factorial using a while loop:
C Program to Find Factorial Using While Loop
#include <stdio.h>
int main() {
int n, fact = 1, i = 1;
printf("Enter a non-negative number: ");
scanf("%d", &n);
if (n < 0) {
printf("Factorial is not defined for negative numbers.\n");
return 1;
}
while (i <= n) {
fact = fact * i;
i++;
}
printf("Factorial of %d = %d\n", n, fact);
return 0;
}
Program Output
When the user enters 5, the output is:
Enter a non-negative number: 5
Factorial of 5 = 120
When the user enters 0:
Enter a non-negative number: 0
Factorial of 0 = 1
When the user enters a negative number:
Enter a non-negative number: -3
Factorial is not defined for negative numbers.
Dry Run: Factorial of 5
Let's trace the loop step by step. Initially, fact = 1 and i = 1.
| Iteration | i | Condition (i <= 5) | fact = fact × i | i++ |
|---|---|---|---|---|
| Start | 1 | true | — | — |
| 1 | 1 | true | 1 × 1 = 1 | 2 |
| 2 | 2 | true | 1 × 2 = 2 | 3 |
| 3 | 3 | true | 2 × 3 = 6 | 4 |
| 4 | 4 | true | 6 × 4 = 24 | 5 |
| 5 | 5 | true | 24 × 5 = 120 | 6 |
| After loop | 6 | false | — | — |
After the fifth iteration, fact becomes 120. The counter i increments to 6,
so the condition i <= 5 becomes false and the loop terminates.
The factorial of 5 is 120.
Step-by-Step Explanation
-
Declare variables: Three integers are declared —
nstores the input,factis initialized to 1 (the running product), andiis initialized to 1 (the counter). -
Read input: The program prompts the user to enter a non-negative number and reads it using
scanf(). - Validate input: If the number is negative, the program prints an error message and exits. Factorial is not defined for negative integers.
-
Start the while loop: The loop runs as long as
i <= n. Because it's a while loop, the condition is checked before each iteration. -
Multiply: Inside the loop,
fact = fact * imultiplies the current product by the counter. This builds the factorial step by step. -
Increment: The counter is increased with
i++. -
Repeat or stop: The loop repeats until
iexceedsn, at which point the condition is false and the loop ends. -
Display result: The final factorial value is printed using
printf().
⚠️ Important: Never initialize fact to 0.
Multiplicative identity is 1, and 0 × anything = 0, which would break the calculation.
Special Cases
Factorial of 0
By mathematical definition, 0! = 1. In this program, when the user enters 0,
the condition i <= 0 is false from the start, so the loop never executes.
The variable fact remains at its initial value of 1, which is the correct answer.
Factorial of Negative Numbers
Factorial is not defined for negative integers. The program checks for this
using if (n < 0) and exits gracefully with an error message.
Large Numbers and Overflow
Factorials grow rapidly. On a typical 32-bit system where int can hold up to 2,147,483,647,
the program can calculate factorials up to 12! = 479,001,600.
For 13! and beyond, int overflows and produces incorrect results.
To handle larger values, use long long int.
Practice Questions
Try these exercises to strengthen your understanding of the factorial program:
- Multiple factorials: Modify the program so the user can calculate factorials of several numbers without restarting.
- Use long long: Rewrite the program using
long long intand test what the largest calculable factorial is on your system. - Factorial using recursion: Rewrite the same program using a recursive function instead of a loop.
- Sum of factorials: Extend the program to calculate 1! + 2! + 3! + ... + n!.
- Input validation loop: Make the program re-prompt the user if they enter a negative number or non-numeric input.
Hint: For each variation, the core multiplication logic stays the same. Only the surrounding structure changes.
Why This Program Matters
- Foundation of loops: The factorial program is one of the cleanest examples of using a while loop with a counter.
- Common in exams: Frequently asked in BCA, MCA, and engineering practical exams.
- Interview favourite: Interviewers often ask to write or modify factorial programs to test loop understanding.
- Foundation for recursion: Once you master the iterative version, understanding recursive factorial becomes much easier.