Java Program to Print a Square Star Pattern (5×5)

star pattern
star pattern

Question :

Write a program to print given pattern !

Program to print given star pattern in Java using simple Trick !
Start pattern program in java

Solution :

Here Is the Java code to print the given pattern.

public class pattern {

	public static void main(String[] args) {
		for (int i = 0; i <5; i++) {
			for (int j = 0; j < 5; j++) {
				System.out.print("*");
			}
			System.out.println();
		}
	}
}

Expected output

*****
*****
*****
*****
*****

How the logic works

The outer loop i runs 5 times (one per row) and the inner loop j runs 5 times (one per column). There is no if condition, so every position prints a star. That gives 5 rows of 5 stars, 25 stars in total. After each row, System.out.println() moves to the next line.

FAQ

Why do these programs use i + j and j – i?
In a grid, every cell on the same anti-diagonal has the same i + j value, and every cell on the same diagonal has the same j - i value. Comparing them with a number draws the straight edges of the shape.

Can I use the same logic in C++ or Python?
Yes. Only the loop and print syntax change. The condition on i and j stays the same.

More star patterns: Star patterns set 1, Star patterns set 2, Star patterns set 3, Star patterns set 4, Star pattern set 5.

Leave a Reply