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

Number-Star Trapezium Pattern (1*2*3*4*17*18*19*20)

Read summary with AI tools

The number-star trapezium pattern is a placement-test favourite: the numbers 1 to 20 are split into a left and a right half, joined with stars, and each row is pushed in by two dashes. Reading the numbers in order means going down the left side and back up the right.

Quick answer: Work out where each half of row i starts. The left half starts at 1 + i·n − i(i − 1)/2 and holds n − i numbers. The right half starts at n(n + 1)/2 + 1 + (n − i − 1)(n − i)/2. Print 2i dashes, then both halves joined with stars.

This program is part of the Tricky Patterns collection.

Table of contents8 sections
  1. Number-Star Trapezium Pattern output
  2. How the number-star trapezium pattern works
  3. Number-Star Trapezium Pattern in C
  4. Number-Star Trapezium Pattern in C++
  5. Number-Star Trapezium Pattern in Java
  6. Number-Star Trapezium Pattern in Python
  7. Time and space complexity
  8. Frequently asked questions

Number-Star Trapezium Pattern output

Output (n = 4):

1*2*3*4*17*18*19*20
--5*6*7*14*15*16
----8*9*12*13
------10*11
Number-Star Trapezium Pattern layout diagram, row by row (n = 4)
How each row of the number-star trapezium pattern is built (n = 4). Blue cells are printed characters; grey dots are spaces.

How the number-star trapezium pattern works

The left halves of all rows use the numbers 1 to 10 (for n = 4) from top to bottom, 4 + 3 + 2 + 1 of them. The right halves use 11 to 20 from bottom to top. So row i‘s left half starts after the numbers used by the rows above it, while its right half starts after the numbers used by the rows below it. Both starting points are sums of a shrinking series, which is where the two formulas come from.

  1. Set total = n(n + 1) / 2, the count of numbers in all the left halves.
  2. For row i from 0 to n − 1, work out left and right with the formulas above.
  3. Print 2i dashes.
  4. Print left to left + n − i − 1, each followed by a star.
  5. Print right to right + n − i − 1 with stars between them, then move to the next line.

Key takeaways

  • The left halves count down the page; the right halves count up it.
  • Each start point is “numbers used so far”, a sum of the row lengths 4, 3, 2, 1.
  • The dashes are part of the required output, not spaces, so they must be printed.
  • Checking your formula against row 0 and the last row catches most mistakes.

Number-Star Trapezium Pattern in C

#include <stdio.h>

int main(void) {
    int n = 4;
    int total = n * (n + 1) / 2;            /* how many numbers the left halves use */

    for (int i = 0; i < n; i++) {
        int left = 1 + i * n - i * (i - 1) / 2;
        int right = total + 1 + (n - i - 1) * (n - i) / 2;
        for (int k = 0; k < 2 * i; k++)
            printf("-");
        for (int k = 0; k < n - i; k++)
            printf("%d*", left + k);
        for (int k = 0; k < n - i; k++) {
            printf("%d", right + k);
            if (k < n - i - 1)
                printf("*");
        }
        printf("\n");
    }
    return 0;
}

Number-Star Trapezium Pattern in C++

#include <iostream>
#include <string>
using namespace std;

int main() {
    int n = 4;
    int total = n * (n + 1) / 2;            // how many numbers the left halves use

    for (int i = 0; i < n; i++) {
        int left = 1 + i * n - i * (i - 1) / 2;
        int right = total + 1 + (n - i - 1) * (n - i) / 2;
        cout << string(2 * i, '-');
        for (int k = 0; k < n - i; k++)
            cout << left + k << "*";
        for (int k = 0; k < n - i; k++) {
            cout << right + k;
            if (k < n - i - 1)
                cout << "*";
        }
        cout << endl;
    }
    return 0;
}

Number-Star Trapezium Pattern in Java

public class NumberStarTrapezium {
    public static void main(String[] args) {
        int n = 4;
        int total = n * (n + 1) / 2;            // how many numbers the left halves use

        for (int i = 0; i < n; i++) {
            int left = 1 + i * n - i * (i - 1) / 2;
            int right = total + 1 + (n - i - 1) * (n - i) / 2;
            StringBuilder row = new StringBuilder("-".repeat(2 * i));
            for (int k = 0; k < n - i; k++)
                row.append(left + k).append("*");
            for (int k = 0; k < n - i; k++) {
                row.append(right + k);
                if (k < n - i - 1)
                    row.append("*");
            }
            System.out.println(row);
        }
    }
}

Number-Star Trapezium Pattern in Python

n = 4
total = n * (n + 1) // 2                # how many numbers the left halves use

for i in range(n):
    left = 1 + i * n - i * (i - 1) // 2
    right = total + 1 + (n - i - 1) * (n - i) // 2
    numbers = [left + k for k in range(n - i)] + [right + k for k in range(n - i)]
    print("-" * (2 * i) + "*".join(str(x) for x in numbers))

Time and space complexity

The program prints n(n + 1) numbers with constant work for each, so it runs in O(n²) time and uses O(1) extra memory (the Python version builds one row at a time).

Frequently asked questions

Is there a way to do it without the formulas?

Yes. Fill two arrays first: the left halves top to bottom with a counter that starts at 1, then the right halves bottom to top with the same counter. Then print the rows. It is easier to follow, but uses extra memory.

Why does the right half of the top row start at 17?

The right halves below it hold 1 + 2 + 3 = 6 numbers (11 to 16), and those come straight after the 10 numbers on the left. So the top right half starts at 10 + 6 + 1 = 17.

Can I print spaces instead of dashes?

Yes, if the question allows it. Some versions of this test use dashes so that the leading part of each row is easy to check; change the "-" to " " and nothing else changes.

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.