The number bridge pattern, also called the number butterfly or mirrored number triangle, prints 1 to i on the left, the same numbers backwards on the right, and a gap between them that closes on the last row.
Quick answer: Each row has three pieces: the numbers 1 to i, then 2(n − i) spaces, then i down to 1. The gap shrinks by two spaces per row, so on row n it disappears and the row becomes the palindrome 1234554321.
This program is part of the Tricky Patterns collection.
Table of contents8 sections
Number Bridge Pattern output
Output (n = 5):
1 1 12 21 123 321 1234 4321 1234554321

How the number bridge pattern works
The left piece is the number right triangle and the right piece is its mirror image. Together they always fill 2n columns: i numbers on each side plus 2(n − i) spaces in the middle. That fixed width is why the right edge stays straight. The right piece prints i − j + 1, which runs from i down to 1 as j goes up.
- Run the outer loop for
ifrom1ton, one row per pass. - Print
inumbers (valuej). - Print
2 × (n − i)spaces. - Print
inumbers (valuei − j + 1). - Move to the next line.
Key takeaways
- Every row is exactly 2n characters wide: numbers, gap, numbers.
- The gap is
2(n − i)spaces, so it shrinks by two per row. - The right side prints
i − j + 1to count down whilejcounts up. - The last row has no gap, so the middle number appears twice: 1234554321.
Number Bridge Pattern in C
#include <stdio.h>
int main(void) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++)
printf("%d", j);
for (int j = 1; j <= 2 * (n - i); j++)
printf(" ");
for (int j = 1; j <= i; j++)
printf("%d", i - j + 1);
printf("\n");
}
return 0;
}Number Bridge Pattern in C++
#include <iostream>
using namespace std;
int main() {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++)
cout << j;
for (int j = 1; j <= 2 * (n - i); j++)
cout << " ";
for (int j = 1; j <= i; j++)
cout << i - j + 1;
cout << endl;
}
return 0;
}Number Bridge Pattern in Java
public class NumberBridge {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++)
System.out.print(j);
for (int j = 1; j <= 2 * (n - i); j++)
System.out.print(" ");
for (int j = 1; j <= i; j++)
System.out.print(i - j + 1);
System.out.println();
}
}
}Number Bridge Pattern in Python
n = 5
for i in range(1, n + 1):
for j in range(1, i + 1):
print(j, end="")
for j in range(1, 2 * (n - i) + 1):
print(" ", end="")
for j in range(1, i + 1):
print(i - j + 1, 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
How do I remove the doubled middle number on the last row?
Print only i − 1 numbers on the right on the last row, or make the gap 2(n − i) − 1 and skip the first number of the right side. The row then reads 123454321.
How do I turn it into a full butterfly?
Add a second loop after the first one that runs i from n − 1 down to 1 with the same three pieces. The shape then opens up again, like the butterfly star pattern with numbers.
Why do the numbers go out of line for n = 10?
Because 10 has two digits. Keep n at 9 or less, or print each number in a fixed width and double the gap.











