Java Pattern Programs – Star, Number and Character Patterns
Pattern programs print shapes such as triangles, pyramids and diamonds using stars, numbers or letters. They are a classic way to master nested loops: the outer loop controls the rows, and inner loops control the spaces and symbols in each row. They are also popular questions in exams and beginner interviews.
This page first explains the technique with runnable star, number and character examples, then provides a gallery of 56 solved patterns, each linking to a full program with output.
1. How Pattern Programs Work
Every pattern follows the same recipe:
- Draw the pattern for a small size (say
n = 5) and number the rows. - For each row, count the leading spaces and the symbols, and express both as formulas of the row number (for a pyramid:
n - rowspaces androwstars). - Write an outer loop over rows and inner loops (or
repeat()) that print those counts, then a line break. - For shapes with a top and bottom half, such as diamonds and sandglasses, print two halves or loop from
-(n-1)ton-1and useMath.abs.
2. Star Patterns
public class StarPatterns {
public static void main(String[] args) {
int n = 5;
System.out.println("Right triangle:");
for (int row = 1; row <= n; row++) { // outer loop: one line per row
for (int col = 1; col <= row; col++) { // inner loop: 'row' stars
System.out.print("* ");
}
System.out.println();
}
System.out.println("Pyramid:");
for (int row = 1; row <= n; row++) {
System.out.print(" ".repeat(n - row)); // leading spaces shrink
System.out.println("* ".repeat(row)); // stars grow
}
System.out.println("Diamond:");
for (int row = -(n - 1); row <= n - 1; row++) { // rows -4..4: distance from the middle
int stars = n - Math.abs(row);
System.out.print(" ".repeat(n - stars));
System.out.println("* ".repeat(stars));
}
}
}
Right triangle:
*
* *
* * *
* * * *
* * * * *
Pyramid:
*
* *
* * *
* * * *
* * * * *
Diamond:
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
String.repeat() (Java 11) replaces an inner loop when a row consists of the same text repeated.
3. Number Patterns
public class NumberPatterns {
public static void main(String[] args) {
int n = 5;
System.out.println("Row numbers:");
for (int row = 1; row <= n; row++) {
for (int col = 1; col <= row; col++) System.out.print(col + " ");
System.out.println();
}
System.out.println("Floyd's triangle:");
int value = 1;
for (int row = 1; row <= n; row++) {
for (int col = 1; col <= row; col++) System.out.printf("%-3d", value++);
System.out.println();
}
System.out.println("Pascal's triangle:");
for (int row = 0; row < n; row++) {
System.out.print(" ".repeat(n - row - 1));
int c = 1; // C(row, 0)
for (int k = 0; k <= row; k++) {
System.out.print(c + " ");
c = c * (row - k) / (k + 1); // C(row, k+1) from C(row, k)
}
System.out.println();
}
}
}
Row numbers:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Floyd's triangle:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Pascal's triangle:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Number patterns print either the column index, a running counter (Floyd's triangle) or a computed value (Pascal's triangle, using binomial coefficients).
4. Character Patterns
public class CharacterPatterns {
public static void main(String[] args) {
int n = 5;
System.out.println("Letters per row:");
for (int row = 0; row < n; row++) {
for (char ch = 'A'; ch <= 'A' + row; ch++) System.out.print(ch + " ");
System.out.println();
}
System.out.println("Same letter per row:");
for (int row = 0; row < n; row++) {
char ch = (char) ('A' + row);
System.out.println((ch + " ").repeat(row + 1));
}
System.out.println("Palindromic pyramid:");
for (int row = 0; row < n; row++) {
StringBuilder line = new StringBuilder(" ".repeat(n - row - 1));
for (char ch = 'A'; ch <= 'A' + row; ch++) line.append(ch);
for (char ch = (char) ('A' + row - 1); ch >= 'A'; ch--) line.append(ch);
System.out.println(line);
}
}
}
Letters per row:
A
A B
A B C
A B C D
A B C D E
Same letter per row:
A
B B
C C C
D D D D
E E E E E
Palindromic pyramid:
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
Characters are numeric codes, so 'A' + row moves forward through the alphabet; cast back with (char) to print a letter.
5. Star Pattern Programs
Click a pattern to see its complete program and output.
Left triangle star pattern
Pyramid star pattern
Right triangle star pattern
Downward left triangle star pattern
Downward pyramid star pattern
Downward right triangle star pattern
Downward pyramid outline star pattern
Pyramid outline star pattern
Right triangle outline star pattern
Left Pascal triangle star pattern
Diamond star pattern
Right Pascal triangle star pattern
Diamond outline star pattern
Sandglass star pattern
6. Number Pattern Programs
Right triangle number pattern (1)
Right triangle number pattern (2)
Right triangle number pattern (3)
Left triangle number pattern (1)
Left triangle number pattern (2)
Left triangle number pattern (3)
Downward right triangle number pattern (1)
Downward right triangle number pattern (2)
Downward right triangle number pattern (3)
Downward left triangle number pattern (1)
Downward left triangle number pattern (2)
Downward left triangle number pattern (3)
Pyramid number pattern (1)
Pyramid number pattern (2)
V number pattern
Downward pyramid number pattern (1)
Diamond number pattern
Double triangle number pattern (1)
Double triangle number pattern (2)
Right Pascal triangle number pattern
Sandglass number pattern
7. Character Pattern Programs
Right triangle character pattern (1)
Right triangle character pattern (2)
Right triangle character pattern (3)
Left triangle character pattern (1)
Left triangle character pattern (2)
Left triangle character pattern (3)
Downward right triangle character pattern (1)
Downward right triangle character pattern (2)
Downward right triangle character pattern (3)
Downward left triangle character pattern (1)
Downward left triangle character pattern (2)
Downward left triangle character pattern (3)
Pyramid character pattern (1)
Pyramid character pattern (2)
Downward pyramid character pattern
Diamond character pattern
Right Pascal triangle character pattern
Double triangle character pattern
Diamond outline character pattern
Left Pascal triangle character pattern
Sandglass character pattern
8. Tips for Solving Any Pattern
- Make a small table of row number, spaces and symbols; the formula usually becomes obvious.
- Use
printinside the row andprintlnonly at the end of each row. - For hollow (outline) patterns, print a symbol only on the borders: first or last column, or last row; print spaces elsewhere.
- Read
nfrom the user with Scanner so the same program prints any size. - Check odd and even sizes and
n = 1as edge cases.
9. Summary
- Pattern programs use an outer loop for rows and inner loops for spaces and symbols.
- Express spaces and symbol counts as formulas of the row number.
- Star, number and character patterns share the same structure; only the printed value changes.
- Practise with the 56 solved patterns above to master nested loops.
Related Tutorials
Frequently Asked Questions (FAQ)
Q1. What are pattern programs in Java?
Programs that print shapes such as triangles, pyramids and diamonds using characters like *, numbers or letters, built with nested loops.
Q2. Why are pattern programs important?
They train you to think in terms of loops and formulas, a core skill for any algorithmic problem, and are common in exams and interviews.
Q3. How do I print a pyramid pattern in Java?
For each row from 1 to n, print n - row spaces followed by row copies of "* ", then a newline.
Q4. How do I print a diamond pattern?
Print a pyramid followed by an inverted pyramid with one row fewer, or loop from -(n-1) to n-1 and compute stars as n - |row|.
Q5. What is Floyd's triangle?
A right triangle of consecutive natural numbers: 1; 2 3; 4 5 6; and so on, printed with a counter that keeps increasing.
Q6. What is Pascal's triangle?
A triangle in which each number is the sum of the two above it; row k contains the binomial coefficients C(k, 0) to C(k, k).
Q7. How do I print letters instead of stars?
Use a char variable starting at 'A' and increment it, or compute (char) ('A' + index).
Q8. How do I print a hollow (outline) pattern?
Print the symbol only on boundary positions (first or last column of a row, or the last row) and spaces elsewhere.
Q9. Should I use for or while loops for patterns?
Either works. for loops are most common because the number of rows and columns is known in advance.
Q10. What is the time complexity of pattern programs?
Typically O(n²) for n rows, because each row prints up to n (or 2n) characters.
Q11. How do I take the number of rows as input?
Read it with Scanner sc = new Scanner(System.in); int n = sc.nextInt(); before the loops.
Q12. What does String.repeat do in pattern programs?
It returns a string repeated a given number of times (Java 11), replacing an inner loop, for example "* ".repeat(row).
Q13. How do I print an inverted triangle?
Run the outer loop from n down to 1, or print n - row + 1 symbols for row 1 to n.
Q14. Why does my pattern look misaligned?
Spaces and symbols must have consistent widths. Multi-digit numbers need padding, for example printf("%-3d", value).
Q15. What is a sandglass pattern?
An inverted pyramid followed by an upright pyramid, so the shape narrows to a point in the middle like an hourglass.
Q16. What is a Pascal triangle star pattern?
On this site it refers to a sideways triangle: the number of stars grows to n and then shrinks, aligned left or right.
Q17. Can pattern programs be solved with recursion?
Yes. A recursive method can print one row and call itself for the next, though loops are simpler and more common.
Q18. How can I build a pattern into a single string?
Append each row to a StringBuilder with '\n' and print once at the end; this is also faster for large patterns.
Q19. Which patterns are asked most in interviews?
Pyramid, diamond, Floyd's triangle, Pascal's triangle, number triangles and hollow squares or pyramids.
Q20. What should I learn after pattern programs?
Arrays and strings, then collections and object-oriented programming, using the same step-by-step problem-solving approach.