Square Pattern in Java

Square pattern must be the most basic patterns of all. Anybody learning about nested loops should try to make a square pattern. Let us see how we can implement it.

public class Tech4Humans {

	public static void main(String[] args) {

		// length of square
		int size = 5;
		
		for (int i = 0; i < size; i++) {
			for (int j = 0; j < size; j++) {
				System.out.print("* ");
			}
			System.out.println();
		}

	}

}

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

To understand the above pattern use pen and paper to iterate through each loop.

After understanding the above logic, try to make a rectangle of length=10 and breadth=5.

Leave a Comment