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

Trapezium Star Pattern

Read summary with AI tools

The trapezium star pattern, called a trapezoid in American English, looks like a pyramid with its top cut off: the first row already has several stars, and every row below is two stars wider.

Quick answer: Row i prints n − i spaces and then 2i + n − 1 stars. The first row starts with n + 1 stars instead of one, which gives the flat top. Each row after that adds one star on each side.

This program is part of the Star Patterns collection.

Table of contents8 sections
  1. Output
  2. How the trapezium star pattern works
  3. Program in C
  4. Program in C++
  5. Program in Java
  6. Program in Python
  7. Time and space complexity
  8. Frequently asked questions

Trapezium Star Pattern output

Output (n = 4):

   *****
  *******
 *********
***********
Trapezium Star Pattern layout diagram, row by row (n = 4)
How each row of the trapezium star pattern is built (n = 4). Blue cells are printed characters; grey dots are spaces.

How the trapezium star pattern works

A full pyramid prints 2i − 1 stars on row i. Adding a constant to that count widens every row by the same amount, which slices the point off the pyramid and leaves a flat top. Here the constant is n, so the top row has n + 1 stars. The indent is the same as in a pyramid, so the two slanted sides stay straight.

  1. Run the outer loop for i from 1 to n, one row per pass.
  2. Print n − i spaces.
  3. Print 2 × i + n − 1 stars.
  4. Move to the next line.

Key takeaways

  • A trapezium is a pyramid with a constant added to every row.
  • The star count 2i + n − 1 grows by two per row, like a pyramid.
  • The indent n − i keeps both sides slanting evenly.
  • Changing the constant changes how wide the flat top is.

Trapezium Star Pattern in C

#include <stdio.h>

int main(void) {
    int n = 4;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n - i; j++)
            printf(" ");
        for (int j = 1; j <= 2 * i + n - 1; j++)
            printf("*");
        printf("\n");
    }
    return 0;
}

Trapezium Star Pattern in C++

#include <iostream>
using namespace std;

int main() {
    int n = 4;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n - i; j++)
            cout << " ";
        for (int j = 1; j <= 2 * i + n - 1; j++)
            cout << "*";
        cout << endl;
    }
    return 0;
}

Trapezium Star Pattern in Java

public class TrapeziumStar {
    public static void main(String[] args) {
        int n = 4;

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n - i; j++)
                System.out.print(" ");
            for (int j = 1; j <= 2 * i + n - 1; j++)
                System.out.print("*");
            System.out.println();
        }
    }
}

Trapezium Star Pattern in Python

n = 4

for i in range(1, n + 1):
    for j in range(1, n - i + 1):
        print(" ", end="")
    for j in range(1, 2 * i + n):
        print("*", 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 make the top wider or narrower?

Replace the n − 1 in the star count with any number k. The top row then has k + 2 stars, and the rest of the shape follows.

How do I turn it upside down?

Run the rows from n down to 1 with the same spaces and stars. The wide side is then on top.

How do I make a hollow trapezium?

Print stars only on the first and last rows and at the two ends of each row. See the hollow trapezium star pattern.

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.