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

Ladder Pattern

The ladder pattern prints two rails of stars with a rung across them every third row. It is a short exercise in combining a column test with a row test.

Quick answer: Use a grid five columns wide. Print a star in the first and last column on every row, and fill the whole row when i % 3 == 0: those rows are the rungs. With n rungs the ladder is 3n + 2 rows tall.

This program is part of the Symbol Patterns collection. The same loops draw the ladder pattern with #, @ or a digit instead of stars: see Ladder Pattern with #, @ or numbers.

Table of contents9 sections
  1. Output
  2. How the ladder pattern works
  3. Program in C
  4. Program in C++
  5. Program in Java
  6. Program in Python
  7. With #, @ or Numbers
  8. Time and space complexity
  9. Frequently asked questions

Ladder Pattern output

Output (3 rungs):

*   *
*   *
*****
*   *
*   *
*****
*   *
*   *
*****
*   *
*   *
Ladder Pattern layout diagram, row by row (3 rungs)
How each row of the ladder pattern is built (3 rungs). Blue cells are printed characters; grey dots are spaces.

How the ladder pattern works

The two rails are a column condition that holds on every row. The rungs are a row condition that holds on every third row. Joining the two with || draws both. Ending with two plain rail rows after the last rung makes the ladder look finished at the bottom, which is where 3n + 2 comes from.

  1. Run the outer loop for i from 1 to 3 × n + 2, one row per pass.
  2. Go through 5 positions in the row. Print stars where j = 1 or j = 5 or i % 3 = 0; print spaces everywhere else.
  3. Move to the next line.

Key takeaways

  • Rails are a column test; rungs are a row test.
  • i % 3 == 0 puts a rung on every third row.
  • Changing the 3 changes the gap between rungs.
  • The two tests are joined with ||, so a cell is a star if either is true.

Ladder Pattern in C

#include <stdio.h>

int main(void) {
    int n = 3;

    for (int i = 1; i <= 3 * n + 2; i++) {
        for (int j = 1; j <= 5; j++) {
            if (j == 1 || j == 5 || i % 3 == 0)
                printf("*");
            else
                printf(" ");
        }
        printf("\n");
    }
    return 0;
}

Ladder Pattern in C++

#include <iostream>
using namespace std;

int main() {
    int n = 3;

    for (int i = 1; i <= 3 * n + 2; i++) {
        for (int j = 1; j <= 5; j++) {
            if (j == 1 || j == 5 || i % 3 == 0)
                cout << "*";
            else
                cout << " ";
        }
        cout << endl;
    }
    return 0;
}

Ladder Pattern in Java

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

        for (int i = 1; i <= 3 * n + 2; i++) {
            for (int j = 1; j <= 5; j++) {
                if (j == 1 || j == 5 || i % 3 == 0)
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            System.out.println();
        }
    }
}

Ladder Pattern in Python

n = 3

for i in range(1, 3 * n + 2 + 1):
    for j in range(1, 6):
        if j == 1 or j == 5 or i % 3 == 0:
            print("*", end="")
        else:
            print(" ", end="")
    print()

Ladder Pattern with #, @ or Numbers

To print the ladder pattern with any symbol, move the character into a variable. The row and column conditions stay exactly the same, so the outline does not change; only what is printed at each filled position does.

Ladder Pattern in Python with any character

n = 3
ch = "#"  # any character: "*", "#", "@", "1" ...

for i in range(1, 3 * n + 2 + 1):
    for j in range(1, 6):
        if j == 1 or j == 5 or i % 3 == 0:
            print(ch, end="")
        else:
            print(" ", end="")
    print()

Output with ch = "#" (3 rungs):

#   #
#   #
#####
#   #
#   #
#####
#   #
#   #
#####
#   #
#   #

Output with ch = "1":

1   1
1   1
11111
1   1
1   1
11111
1   1
1   1
11111
1   1
1   1

Ladder Pattern in C, C++ and Java with any character

Start from the full program in the tabs above and change these lines. Use single quotes for a char: '#', '@' or '1'.

C

char ch = '#';     // add after: int n = 3;
printf("%c", ch);  // was: printf("*");

C++

char ch = '#';  // add after: int n = 3;
cout << ch;     // was: cout << "*";

Java

char ch = '#';         // add after: int n = 3;
System.out.print(ch);  // was: System.out.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 ladder wider?

Replace the 5 with a variable w and test j == w for the right rail.

How do I add more space between rungs?

Use i % 4 == 0 for a rung every fourth row and make the height 4n + 3.

Why does the ladder end with two rail rows?

The height is 3n + 2, so after the last rung on row 3n there are two more rows. They show the bottom of the rails, the same as the two rows above the first rung.

How do I print the rungs with a different symbol?

Test the rung rows first and print = there, then print the rails with stars on the other rows.

This tutorial was drafted with AI assistance. Spotted a mistake? Tell us and we fix it. Read our editorial policy.

Quick check: can you predict the output?

Answer from the code, then check. Every answer comes from the real output above.

Pattern 12 of 17 in Symbol Patterns

 

Still stuck? Ask AI to explain this code

Opens your favourite AI with this page, ready to explain every loop step by step.

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.

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.