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

Number Bridge Pattern (1 1, 12 21, 123 321)

Read summary with AI tools

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
  1. Number Bridge Pattern output
  2. How the number bridge pattern works
  3. Number Bridge Pattern in C
  4. Number Bridge Pattern in C++
  5. Number Bridge Pattern in Java
  6. Number Bridge Pattern in Python
  7. Time and space complexity
  8. Frequently asked questions

Number Bridge Pattern output

Output (n = 5):

1        1
12      21
123    321
1234  4321
1234554321
Number Bridge Pattern layout diagram, row by row (n = 5)
How each row of the number bridge pattern is built (n = 5). Blue cells are printed characters; grey dots are spaces.

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.

  1. Run the outer loop for i from 1 to n, one row per pass.
  2. Print i numbers (value j).
  3. Print 2 × (n − i) spaces.
  4. Print i numbers (value i − j + 1).
  5. 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 + 1 to count down while j counts 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.

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.