Floyd’s pyramid prints the numbers of Floyd’s triangle in a centred pyramid instead of a right triangle. The numbers still run 1 to 15 row by row; only the layout changes.
Quick answer: Row i holds the numbers from i(i â 1)/2 + 1 to i(i + 1)/2. Print each one in a field four characters wide and put two spaces per missing number in front of the row, n â i pairs in all. Half the field width per step keeps every row centred.
This program is part of the Pyramid Patterns collection.
Table of contents8 sections
Floyd’s Pyramid output
Output (n = 5):
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
How the floyd’s pyramid works
Centring a row of numbers works when every number takes the same width. With a width of 4, a row with i numbers is 4i characters wide, and each row is 4 characters wider than the one above. Shifting each shorter row right by half of that, 2 spaces per step, puts all the rows on one centre line. The values come from Floyd’s formula: the rows above row i use i(i â 1)/2 numbers.
- Run the outer loop for
ifrom1ton, one row per pass. - Print
n â ispaces. - Print
inumbers (valuei Ă (i â 1) / 2 + j). - Move to the next line.
Key takeaways
- The first number on row
iisi(i â 1)/2 + 1. - A fixed width per number is what makes centring possible.
- The indent is half the field width per missing number: 2 spaces for a width of 4.
- Floyd’s triangle and Floyd’s pyramid print the same numbers; only the spacing differs.
Floyd’s Pyramid in C
#include <stdio.h>
int main(void) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++)
printf(" ");
for (int j = 1; j <= i; j++)
printf("%4d", i * (i - 1) / 2 + j);
printf("\n");
}
return 0;
}Floyd’s Pyramid in C++
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++)
cout << " ";
for (int j = 1; j <= i; j++)
cout << setw(4) << i * (i - 1) / 2 + j;
cout << endl;
}
return 0;
}Floyd’s Pyramid in Java
public class FloydsPyramid {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++)
System.out.print(" ");
for (int j = 1; j <= i; j++)
System.out.printf("%4d", i * (i - 1) / 2 + j);
System.out.println();
}
}
}Floyd’s Pyramid in Python
n = 5
for i in range(1, n + 1):
for j in range(1, n - i + 1):
print(" ", end="")
for j in range(1, i + 1):
print(f"{i * (i - 1) // 2 + j:4d}", end="")
print()Time and space complexity
The program prints one character for each position in the pattern, so for n rows it runs in O(nÂČ) time. It only needs a few loop variables, so the extra memory is O(1).
Frequently asked questions
Why use a width of 4?
It leaves at least one space between neighbours up to 999. For five rows a width of 3 would also work, but 4 keeps the shape when you raise n to 10 or more.
Can I use a counter instead of the formula?
Yes. Start a counter at 1 before the loops and print counter++. The formula shows how many numbers come before each row, which is useful when you only need one row.
How do I let the user choose the number of rows?
Read n from the keyboard instead of fixing it in the code: scanf("%d", &n); in C, cin >> n; in C++, new Scanner(System.in).nextInt() in Java and int(input()) in Python.






