The factorial of a nonnegative integer n, written n!, is the product of every integer from 1 through n. By definition, 0! = 1.
Remember
n! = 1 × 2 × 3 × … × n (for n ≥ 1)
0! = 1
Example: 5!
5 × 4 × 3 × 2 × 1 = 120
Recurrence:
n! = n × (n − 1)!
with base case 0! = 1! = 1
n
Expansion
n!
0
(empty product)
1
1
1
1
5
5 × 4 × 3 × 2 × 1
120
6
6 × 5!
720
Approach
How to Find Factorial in C
Four clear steps — then pick a loop or recursion.
Reject negative inputs (factorial is not defined for them).
Treat 0 and 1 as the base case: return 1.
Multiply all integers from 2 through n (loop), or use n × (n − 1)! (recursion).
Remember unsigned long long is exact only through 20!.
Method
Idea
Time
Best for
Iterative loop
Multiply 2..n into a running product
O(n)
Production, large n, interviews
Recursion
n × factorial(n − 1) with base ≤ 1
O(n)
Showing the recurrence clearly
Pseudocode
Pseudocode
function Factorial(n):
if n < 0:
error "not defined"
if n <= 1:
return 1
result = 1
for i from 2 to n:
result = result * i
return result
Reference
Cheat Sheet
Keep these facts handy while writing the program.
Topic
Detail
Definition
Product of 1..n; 0! = 1
Base cases
0 and 1 both return 1
Recurrence
n! = n × (n − 1)!
Type tip
unsigned long long exact through 20!
Print
Use %llu with unsigned long long
Classic check
5! → 120
Try it
Live Preview
Enter a nonnegative integer (0–20) to see the product chain and n!.
Whole numbers from 0 to 20 (exact unsigned long long range). Tap a chip or type a value.
Result
n = 5
Chain: 1 × 2 × 3 × 4 × 5
5! = 120
Trace
Worked Walkthrough — n = 5
Start at 1 and multiply each next integer until you reach 5.
Calculation
result = 1
i = 2 → result = 1 × 2 = 2
i = 3 → result = 2 × 3 = 6
i = 4 → result = 6 × 4 = 24
i = 5 → result = 24 × 5 = 120
Stop (i reached n)
5! = 120
Recursive view:
5! = 5 × 4!
4! = 4 × 3!
3! = 3 × 2!
2! = 2 × 1!
1! = 1
→ 120
Code
C Programs
Start with the iterative loop for interviews and real code. Use recursion when you want to show the recurrence clearly.
Example 1 — Iterative Loop
Multiply integers from 2 to n into a running product. No recursion-depth risk.
C
#include <stdio.h>
unsigned long long factorial(int n)
{
unsigned long long result = 1;
int i;
if (n < 0)
{
return 0; /* signal invalid input */
}
for (i = 2; i <= n; i++)
{
result *= (unsigned long long)i;
}
return result;
}
int main(void)
{
int number = 5;
if (number < 0)
{
printf("Factorial is not defined for negatives.\\n");
return 1;
}
printf("%d! = %llu\\n", number, factorial(number));
return 0;
}
Output
5! = 120
How It Works
Reject n < 0 — integer factorial is undefined there.
Start with result = 1 (covers both 0! and 1!).
Loop i from 2 to n and multiply into result.
For n = 5: 1 → 2 → 6 → 24 → 120.
Print with %llu because the return type is unsigned long long.
Example 2 — Recursion
Direct translation of n! = n × (n − 1)! with base cases 0 and 1.
C
#include <stdio.h>
unsigned long long factorial(int n)
{
if (n < 0)
{
return 0; /* signal invalid input */
}
if (n <= 1)
{
return 1;
}
return (unsigned long long)n * factorial(n - 1);
}
int main(void)
{
printf("%llu\\n", factorial(5)); /* 120 */
printf("%llu\\n", factorial(0)); /* 1 */
return 0;
}
Output
120
1
How It Works
Validate n >= 0 first.
Base case: if n <= 1, return 1 (stops the recursion).
Otherwise return n * factorial(n - 1).
For 5: calls unwind as 5×4×3×2×1 = 120.
Uses O(n) stack frames — fine for interviews; use a loop for large n.
Edge Cases & Pitfalls
Check these before calling the solution done.
0! / 1!
Both equal 1
The loop body never runs; recursion hits the base case immediately.
Negatives
Reject early
Do not silently return a wrong product — validate n >= 0.
Overflow
ull maxes at 20!
21! does not fit in unsigned long long.
Recursion
Stack depth
Deep recursion can overflow the call stack — prefer a loop.
Analysis
Time and Space Complexity
Program
Time
Extra space
Iterative loop
O(n)
O(1)
Recursion
O(n)
O(n) call stack
Both need about n multiplications. Space differs: the loop keeps one product; recursion keeps one frame per level.