Top 15 Pattern Programs in Java: Star, Number and Character Patterns
Pattern questions appear repeatedly in Java interviews, and many developers find them hard to solve. This guide covers the top 15 pattern programs in Java — star patterns, number patterns and character patterns — each with code and a clear explanation so you grasp the logic rather than memorising it.
If you would rather build your Java foundation first, read Java from JavaGoal.com and then come back to these pattern exercises.
Star Patterns in Java
1. Pyramid Program in Java
The pyramid program prompts the user for a number of rows and then prints a centred pyramid of asterisks. A Scanner object reads the input, an outer loop handles each row, one inner loop prints the leading spaces and a second inner loop prints the stars.
Line-by-line explanation:
- import java.util.Scanner; brings in the Scanner class from the java.util package so the program can read what the user types.
- public class Pyramid declares a public class named Pyramid.
- public static void main(String[] args) is the entry point where execution begins.
- Scanner input = new Scanner(System.in); creates a Scanner object called input that reads from the console.
- System.out.print(...) displays a prompt asking the user how many rows the pyramid should have.
- int rows = input.nextInt(); reads an integer from the user and stores it in the variable rows.
- for (int i = 1; i <= rows; i++) is the outer loop that walks through every row of the pyramid.
- for (int j = 1; j <= rows - i; j++) is a nested loop that prints the spaces before the stars; the count equals the total rows minus the current row number.
- System.out.print(" "); outputs a single space.
- for (int k = 1; k <= 2 * i - 1; k++) is another nested loop that prints the asterisks for the current row, using twice the row number minus one.
- System.out.print("*"); outputs a single asterisk.
- System.out.println(); moves the cursor to the next line so the following row starts fresh.
In short, the program reads a row count from the user, builds a star pyramid with that many rows and prints it to the console using nested loops whose space and star counts depend on the current row.
2. Right Triangle Star Pattern in Java
This program requests a row count, reads it with a Scanner and prints a right-angled triangle of stars using nested loops.
- The Scanner class is imported from java.util so console input can be read.
- Inside main(), a Scanner object is created for reading input.
- The program prompts the user to enter the number of rows for the right triangle using print().
- The entered value is stored in the variable rows.
- The outer loop iterates over each row, starting at i=1 and running while i is less than or equal to rows.
- The first inner loop prints the spaces before the stars, starting at j=1 and running while j is less than or equal to (rows - i).
- The second inner loop prints the stars, starting at k=1 and running while k is less than or equal to i.
- System.out.println() moves the cursor to the next line after the inner loops finish.
- The program then closes the main() method and the class definition.
3. Left Triangle Star Pattern in Java
Here a Scanner reads the row count, an outer loop walks through each row, and an inner loop prints a star followed by a space for every column in that row. After each row the cursor moves to the next line, and the scanner is closed at the end.
4. Diamond Shape Pattern Program in Java
This program prompts the user for a row count, reads it with a Scanner and prints a diamond of stars using nested loops. The row count is split into two halves: the upper half is built with one set of loops that print spaces and stars, and the lower half is built with a second set that mirrors the first.
- import java.util.Scanner; imports the Scanner class from java.util so user input can be read.
- public class DiamondShapePattern begins the class definition.
Stay Connected with Javagoal
Keep learning with us by following our community channels.
Explore More Java Tutorials
Build stronger fundamentals with these topic guides from the Javagoal library.
- Home
- Javagoal Category
- What Is Java Language
- Java Features and Advantages
- Java Hello World Program
- Difference Between JDK, JRE and JVM
- Java Virtual Machine
- Variables in Java
- Local Variable Type Inference (var)
- Data Types in Java
- Java Keywords
- Operators in Java
- What Is a Method in Java
- Control Structures in Java
- Control Statements in Java
- If Statement in Java
- Switch Statement in Java
- Loops in Java
- Break Statement in Java
- Continue Statement in Java
- Java Array
- OOPs Concepts in Java
- Class and Object in Java
- Constructor in Java
- Constructor Overloading Program
- Constructor Chaining in Java
- Java Inheritance
- Multiple Inheritance in Java
- Polymorphism in Java
- Method Overloading in Java
- Method Overriding in Java
- Abstraction in Java
- Abstract Class in Java
- Interface in Java
- Multiple Inheritance Using Interface
- Encapsulation in Java
- Association in Java
- Access Modifiers in Java
- Packages in Java
- Java Import Package
- Enum in Java
- Wrapper Class in Java
- Immutable Class in Java
- Final Keyword in Java
- Java Final Variable
- Final Method in Java
- Final Class in Java
- Final, Finally and Finalize
- Static Keyword in Java
- Static Variable in Java
- Static Method in Java
- Static Block in Java
- Static Class in Java
- This Keyword in Java
- Java Super Keyword
- Import in Java
- Nested Classes in Java
Leave a Reply
Working through these pattern programs is a practical way to sharpen your grasp of nested loops and console input in Java. Try changing the row count or swapping stars for numbers and characters to see how the same loop structure adapts. Once the logic feels familiar, you can move on to the core Java topics linked below.
