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
Ladder Pattern output
Output (3 rungs):
* * * * ***** * * * * ***** * * * * ***** * * * *

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.
- Run the outer loop for
ifrom1to3 × n + 2, one row per pass. - Go through
5positions in the row. Print stars wherej = 1 or j = 5 or i % 3 = 0; print spaces everywhere else. - Move to the next line.
Key takeaways
- Rails are a column test; rungs are a row test.
i % 3 == 0puts 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.






