[Java] Square Pattern Java Program

In this “Print Square Pattern” – We have written Java programs to print/draw square pattern by using different types of Java loops and you can execute and try these Java program through your IDE. This is most [100+] Frequently Asked Java Program on beginner level to check you Java Programming skills and Concept.

Square Pattern Example

This post covers following ways to print Star (X) Pattern:

  • Print Square Pattern Java Program: Using for Loop
  • Print Square Pattern Java Program: Using While Loop
  • Print Square Pattern Java Program: Using Do-While Loop

Logic: A square will have same number of rows and columns. In this program implemented the logic through outer and inner loop. Outer loop is used for rows and inner loop for columns.

Square Pattern Java Program: For Loop

This Square Pattern Java Program is following the below logic:

  1. The outer for loop iterated n times until the condition i<n is false.
  2. The inner for loop iterates n times and display the symbol until the condition j<n is false.
package _1_patterns.square;

import java.util.Scanner;

public class SquarePatternForLoop {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		System.out.println("Enter Pattern Size : ");
		int size = sc.nextInt();

		System.out.print("Enter Symbol : ");
		char symbol = sc.next().charAt(0);

		drawSquarePattern(size, symbol);

		// Close Scanner
		sc.close();
	}

	private static void drawSquarePattern(int n, char c) {
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				System.out.print(c);
			}
			System.out.println();
		}
	}
}

Output

Enter Pattern Size : 
10
Enter Symbol : ^
^^^^^^^^^^
^^^^^^^^^^
^^^^^^^^^^
^^^^^^^^^^
^^^^^^^^^^
^^^^^^^^^^
^^^^^^^^^^
^^^^^^^^^^
^^^^^^^^^^
^^^^^^^^^^

Square Pattern Java Program: While Loop

This Square Pattern Java Program is following the below logic:

  1. The outer while loop iterated n times until the condition i++<n is false.
  2. The inner while loop iterates n times and display the symbol until the condition j++<n is false.
package _1_patterns.square;

import java.util.Scanner;

public class SquarePatternWhileLoop {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		System.out.println("Enter Pattern Size : ");
		int size = sc.nextInt();

		System.out.print("Enter Symbol : ");
		char symbol = sc.next().charAt(0);

		drawSquarePattern(size, symbol);

		// Close Scanner
		sc.close();
	}

	private static void drawSquarePattern(int n, char c) {
		int i = 0;
		while (i++ < n) {
			int j = 0;
			while (j++ < n)
			{
				System.out.print(c);
			}
			System.out.println();
		}
	}
}

Output

Enter Pattern Size : 
8
Enter Symbol : %
%%%%%%%%
%%%%%%%%
%%%%%%%%
%%%%%%%%
%%%%%%%%
%%%%%%%%
%%%%%%%%
%%%%%%%%

Square Pattern Java Program: Do-While Loop

This Square Pattern Java Program is following the below logic:

  1. The outer do-while loop iterated n times until the condition ++i<n is false.
  2. The inner do-while loop iterates n times and display the symbol until the condition ++j<n is false.

package _1_patterns.square;

import java.util.Scanner;

public class SquarePatternDoWhileLoop {

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    System.out.println("Enter Pattern Size : ");
    int size = sc.nextInt();

    System.out.print("Enter Symbol : ");
    char symbol = sc.next().charAt(0);

    drawSquarePattern(size, symbol);

    // Close Scanner
    sc.close();
}

private static void drawSquarePattern(int n, char c) {
       int i = 0;
		do {
			int j = 0;
			do {
				System.out.print(c);
			} while (++j < n);
			System.out.println();
		} while (++i < n);
}
}

Output

Enter Pattern Size : 
7
Enter Symbol : #
#######
#######
#######
#######
#######
#######
#######

Hope this post helps you to implement the Square Pattern through Java Program. Please share your comments.

Happy Learning!!!