The factorial triangle prints 1!, 2!, 3! and so on, with row i showing the factorials up to i!. Five rows end with 5! = 120.
Quick answer: Keep a running product. Start each row with fact = 1, then for j from 1 to i multiply fact by j and print it. Because factorials grow very fast, store the product in a long long in C and C++ and a long in Java.
This program is part of the Series Patterns collection.
Table of contents8 sections
Factorial Triangle output
Output (n = 5):
1 1 2 1 2 6 1 2 6 24 1 2 6 24 120

How the factorial triangle works
j! is (j − 1)! multiplied by j. So there is no need to restart the multiplication for every number: the inner loop keeps the last factorial and multiplies it by the next j, which gives 1, 2, 6, 24, 120 one after another. Resetting the product at the start of each row makes every row begin at 1! again.
- Run the outer loop for
ifrom 1 ton. - Set
fact = 1. - For
jfrom 1 toi: multiplyfactbyjand print it four characters wide. - Move to the next line.
Key takeaways
- Each factorial is the previous one times
j, so the triangle needs only one multiplication per number. - Factorials outgrow an
intat 13!, which is why the product is a 64-bit type. - Resetting
factinside the outer loop makes every row start again at 1. - Widen the field from 4 to 8 or more if you print more than six rows.
Factorial Triangle in C
#include <stdio.h>
int main(void) {
int n = 5;
for (int i = 1; i <= n; i++) {
long long fact = 1;
for (int j = 1; j <= i; j++) {
fact *= j;
printf("%4lld", fact);
}
printf("\n");
}
return 0;
}Factorial Triangle in C++
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int n = 5;
for (int i = 1; i <= n; i++) {
long long fact = 1;
for (int j = 1; j <= i; j++) {
fact *= j;
cout << setw(4) << fact;
}
cout << endl;
}
return 0;
}Factorial Triangle in Java
public class FactorialTriangle {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
long fact = 1;
for (int j = 1; j <= i; j++) {
fact *= j;
System.out.printf("%4d", fact);
}
System.out.println();
}
}
}Factorial Triangle in Python
n = 5
for i in range(1, n + 1):
fact = 1
for j in range(1, i + 1):
fact *= j
print(f"{fact:4d}", end="")
print()Time and space complexity
The program does one multiplication per printed number, so it runs in O(n²) time with O(1) extra memory.
Frequently asked questions
How many rows can I print before the numbers overflow?
12! = 479,001,600 is the largest factorial that fits in a 32-bit int, and 20! is the largest that fits in a 64-bit long long. So the program works for up to 20 rows in C, C++ and Java. Python has no fixed limit.
Why does the C program print with %lld?
%lld is the format for a long long. Printing it with %d reads the wrong number of bytes, and the output is wrong as soon as the value passes about two billion.
How do I print the factorial values as a single column instead?
Drop the outer loop: run one loop for j from 1 to n, multiply, and print each fact on its own line. The triangle is that list printed again and again, one item longer each time.










