
Pattern Programs in Java : Techsnap Guide
Pattern programs are one of those topics that look simple until you actually try them.
A teacher asks for a star triangle. An interviewer asks for a number pyramid. A coding test asks for a hollow diamond. Suddenly, the screen is full of spaces, stars, rows, and nested loops, and the whole thing feels more confusing than it should.
That confusion is normal.
Pattern programs are not really about stars or numbers. They are about learning how loops think. Once you understand how the outer loop controls rows and the inner loop controls what prints inside each row, the rest becomes much easier. That is why pattern problems show up so often in Java practice, lab exams, and interviews.
This guide covers the most common Java patterns with clean examples, simple explanations, and output visuals. Try each one in the Techsnap compiler, change the value of n, and see how the shape changes.
The Core Idea Behind Every Pattern
Every pattern starts with the same question:
What should appear on each row?
For most patterns, you need to figure out three things:
- how many spaces should print before the characters,
- how many stars, numbers, or letters should print,
- whether the pattern grows, shrinks, or does both.
The outer loop usually handles the rows. The inner loop handles the columns or the characters in that row.
For example, if you want a right triangle of stars for n = 5, the output looks like this:
* ** *** **** *****That means row 1 prints 1 star, row 2 prints 2 stars, row 3 prints 3 stars, and so on. The logic is simple once you see it that way.
1. Star Patterns in Java
Star patterns are the most common starting point because they make the loop structure easy to see.
Right-Angled Triangle
For n = 5, the output is:
* ** *** **** *****
import java.util.Scanner; public class RightAngleStarTriangle { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the value of n: "); int n = sc.nextInt(); for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { System.out.print("*"); } System.out.println(); } sc.close(); } }Here, the outer loop controls the rows using i. The inner loop runs from 1 to i, so the number of stars keeps increasing one row at a time.
Centered Star Pyramid
For n = 5, the output is:
* *** ***** ******* *********
import java.util.Scanner; public class StarPyramid { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the value of n: "); int n = sc.nextInt(); 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 - 1); j++) { System.out.print("*"); } System.out.println(); } sc.close(); } }The first loop prints spaces. The second loop prints stars. The formula 2 * i - 1 is important because it gives the odd-number sequence 1, 3, 5, 7, 9.
Diamond Pattern
A diamond is just two pyramids joined together.
* *** ***** ******* ********* ******* ***** *** *
import java.util.Scanner; public class StarDiamond { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the value of n: "); int n = sc.nextInt(); 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 - 1); j++) { System.out.print("*"); } System.out.println(); } for (int i = n - 1; i >= 1; i--) { for (int j = 1; j <= n - i; j++) { System.out.print(" "); } for (int j = 1; j <= (2 * i - 1); j++) { System.out.print("*"); } System.out.println(); } sc.close(); } }The upper half grows. The lower half shrinks. The middle row appears only once.
2. Number Patterns in Java
Number patterns are useful because they force you to track values carefully instead of just printing symbols.
Left-Aligned Number Triangle
For n = 5, the output is:
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
import java.util.Scanner; public class NumberTriangle { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the value of n: "); int n = sc.nextInt(); for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { System.out.print(j + " "); } System.out.println(); } sc.close(); } }The inner loop prints numbers from 1 to the current row number.
Centered Number Pyramid
For n = 5, the output looks like this:
1 121 12321 1234321 123454321
import java.util.Scanner; public class NumberPyramid { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the value of n: "); int n = sc.nextInt(); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n - i; j++) { 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(); } sc.close(); } }This pattern is a good one for learning how to combine increasing and decreasing logic in the same row.
3. Character Patterns in Java
Character patterns are almost the same as number patterns. The only difference is that you print letters instead of digits.
Alphabet Triangle
For n = 5, the output is:
A A B A B C A B C D A B C D E
import java.util.Scanner; public class CharacterTriangle { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter the value of n: "); int n = sc.nextInt(); for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { System.out.print((char) ('A' + j - 1) + " "); } System.out.println(); } sc.close(); } }The expression (char) ('A' + j - 1) converts a number into a letter. When j = 1, the output is A. When j = 2, it is B. That pattern continues naturally.
The Mental Model That Makes Patterns Easy
Do not try to memorize every pattern.
Instead, break the output into rows.
For each row, ask these questions:
- How many spaces should come first?
- How many stars, numbers, or letters should print?
- Does the row grow or shrink?
- Does the shape need a second half?
- Is there a border condition?
Once you answer those questions, the code becomes much easier.
For example:
Spaces = n - i Stars = 2 * i - 1That one formula solves many pyramid-style patterns.
For a right triangle:
Stars = iFor a hollow rectangle:
Print stars only on the border.That is the real skill behind pattern programs. Not memorizing shapes. Learning how to think in rows, columns, and conditions.
Practice These Patterns on Techsnap
Pattern programs are one of the best ways to get comfortable with nested loops in Java.
They help you build logic, improve your speed, and prepare for interviews. They also make it easier to move into arrays, strings, methods, and more advanced problems later.
Open the Techsnap compiler, try these patterns with different values of n, and see how the output changes when you tweak one line of code.
If you want to test your understanding, go through Techsnap practice problems and compare your attempts with the expected output.
Start with one pattern.
Then make it better.
Then solve the next one.
Join Techsnap Creators
Share your knowledge and earn 🚀
Want to showcase your tech expertise and get rewarded for your insights? Join the Techsnap creator network!
Write insightful blogs, stay ahead of industry trends, and grow your professional brand while helping others in the community.
Ready to make an impact?

Comments