Pattern Programs in Java with Explanation
Print common number and alphabet patterns for n rows using nested loops.
- Time
O(n^2) for each pattern - Space
O(1) - Hints3
- Read4 min
Pattern programs in Java use nested loops. The outer loop picks the row, the inner loops print the spaces and values of that row, and println() ends the line. To solve any pattern, write a small table of row number, spaces and values, then turn each column of the table into a formula.
Problem statement
Pattern programs in Java ask you to print a shape made of numbers, letters or
symbols. Given n, print each pattern below with loops only, with no fixed strings
for whole rows.
This page covers number and alphabet patterns. For stars, see star pattern programs. For the full diamond shape, see diamond pattern.
An integer n, the number of rows.
The pattern, one row per line.
Examples
n = 3, number triangle1 / 1 2 / 1 2 3Row i prints the numbers 1 to i. A slash marks a new line here.
n = 3, repeated lettersA / B B / C C CRow i prints the i-th letter i times.
Constraints
- 1 <= n <= 9 for number patterns. Floyd's triangle goes past one digit from row 4
- 1 <= n <= 26 for alphabet patterns
Think before you code
Reveal the questions to ask yourself first
- How many rows are there? That is the outer loop.
- In row
i, how many spaces and how many values are printed? - Does the printed value depend on the row, the column, or a running counter?
Hints
Open one at a time — try again after each.1 Hint 1
2 Hint 2
3 Hint 3
Approach
Reveal the step-by-step approach
Pattern programs in Java: tricks that work every time
1. Outer loop = rows. Inner loops = what one row prints.
2. Make a table. For the palindrome pyramid with n = 4:
| Row i | Spaces | Numbers going up | Numbers going down |
|---|---|---|---|
| 1 | 3 | 1 | none |
| 2 | 2 | 1 2 | 1 |
| 3 | 1 | 1 2 3 | 2 1 |
| Formula | n - i | 1 to i | i - 1 to 1 |
3. Choose what to print:
| What you see | |
|---|---|
| Same value across a row | i |
| Value grows across a row | j |
| Value never restarts | a counter outside both loops |
| 0 and 1 alternate | (i + j) % 2 |
| Letters | (char) ('A' + number) |
4. Call println() after the inner loops, once per row.
If loops are new to you, start with loops in Java.
Dry run
Walk through the example step by step
Floyd's triangle, n = 3. The counter k starts at 1 and is never reset.
i | j values | printed | k after the row
--+----------+---------+----------------
1 | 1 | 1 | 2
2 | 1 2 | 2 3 | 4
3 | 1 2 3 | 4 5 6 | 7
Solution
Reveal the full Java solution
public class NumberPatterns {
// 1. Number triangle
static void triangle(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + " ");
}
System.out.println();
}
}
// 2. Same number in a row
static void repeated(int n) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i + " ");
}
System.out.println();
}
}
// 3. Floyd's triangle
static void floyd(int n) {
int k = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(k++ + " ");
}
System.out.println();
}
}
// 4. Palindrome number pyramid
static void palindrome(int n) {
for (int i = 1; i <= n; i++) {
for (int s = 1; s <= n - i; s++) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print(j + " ");
}
for (int j = i - 1; j >= 1; j--) {
System.out.print(j + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
triangle(4);
System.out.println("---");
repeated(4);
System.out.println("---");
floyd(4);
System.out.println("---");
palindrome(4);
}
}Output:
1
1 2
1 2 3
1 2 3 4
---
1
2 2
3 3 3
4 4 4 4
---
1
2 3
4 5 6
7 8 9 10
---
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
Pattern programs in Java: explanation of each one
- Number triangle: values grow across the row, so print
j. - Repeated number: all values in a row match, so print
i. - Floyd's triangle: the count continues to the next row, so use
koutside the loops. - Palindrome pyramid: three inner loops, one each for spaces, up and down. Each number takes two characters, so each gap is two spaces.
Pattern program in Java using for loop: letters and binary
public class LetterPatterns {
public static void main(String[] args) {
int n = 4;
// 5. Letters across the row
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
System.out.print((char) ('A' + j) + " ");
}
System.out.println();
}
System.out.println("---");
// 6. Same letter in a row
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
System.out.print((char) ('A' + i) + " ");
}
System.out.println();
}
System.out.println("---");
// 7. Binary triangle
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print((i + j + 1) % 2 + " ");
}
System.out.println();
}
System.out.println("---");
// 8. Inverted number triangle
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print(j + " ");
}
System.out.println();
}
}
}
A
A B
A B C
A B C D
---
A
B B
C C C
D D D D
---
1
0 1
1 0 1
0 1 0 1
---
1 2 3 4
1 2 3
1 2
1
In the binary triangle the first value of row 1 must be 1. With i = 1, j = 1,
(1 + 1 + 1) % 2 is 1, and it flips each time j moves by one.
Pattern program in Java: practice list
Try these without looking at the code above:
- Rows
4 3 2 1,4 3 2,4 3,4. - Rows
1,2 4,3 6 9,4 8 12 16. Printi * j. - Rows
D C B A,D C B,D C,D. - A 4 by 4 grid of 0 and 1 like a chess board.
- The Pascal triangle.
For more loop practice, use the core Java practice problems. To learn with a teacher, see the Java full stack course.
O(n^2) for each patternO(1)Review before the interview
Common mistakes
- Calling println() inside the inner loop, which puts each value on its own line.
- Resetting the counter inside the outer loop in Floyd's triangle.
- Writing 'A' + j without the (char) cast, which prints a number such as 65.
- Using one space of indent when each value is two characters wide.
Edge cases to test
- n = 1 prints a single value.
- n = 0 prints nothing.
- Values of 10 or more break the alignment. Use printf with a fixed width.
- More than 26 letters runs past Z.
Interview follow-ups
- How would you keep columns aligned when numbers reach two digits?
- Can you print Floyd's triangle with a formula and no counter?
- How would you write one of these with a while loop?
Keep practising
Star Pattern Programs in Java
Hints · Dry run · SolutionSolve Similar problemPascal Triangle in Java
Hints · Dry run · SolutionSolve Similar problemFizzBuzz Problem
Hints · Dry run · SolutionSolve Related learningLoops
TutorialLearn Related learningControl Statements
TutorialLearn Related learningCoding Questions
TutorialLearn CareerPilot AIPractice under pressure
Take an AI mock interview and get question-by-question feedback.
Mock interviewStartPractising for Java interviews?
CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.
