New: 201 pattern programs with tested code in C, C++, Java and Python Explore patterns

Factorial Triangle (1, 1 2, 1 2 6)

Read summary with AI tools

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
  1. Factorial Triangle output
  2. How the factorial triangle works
  3. Factorial Triangle in C
  4. Factorial Triangle in C++
  5. Factorial Triangle in Java
  6. Factorial Triangle in Python
  7. Time and space complexity
  8. Frequently asked questions

Factorial Triangle output

Output (n = 5):

   1
   1   2
   1   2   6
   1   2   6  24
   1   2   6  24 120
Factorial Triangle layout diagram, row by row (n = 5)
The numbers of the factorial triangle laid out row by row (n = 5).

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.

  1. Run the outer loop for i from 1 to n.
  2. Set fact = 1.
  3. For j from 1 to i: multiply fact by j and print it four characters wide.
  4. 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 int at 13!, which is why the product is a 64-bit type.
  • Resetting fact inside 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.

Authored by

Ghanendra Yadav runs Pattern Programs from Bangalore, India. The site started in 2020 to help students practise loops and logic through pattern programs in C, C++, Java and Python. Every program published here is compiled and run before it goes live, and the output shown is the real output.

Latest articles

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.