Square Star Pattern Program In Java

In Java programming to print square star pattern we need to use loop.

We use all three loops to print star pattern.

Lets see one by one

1 Square Star Pattern Program using For loop

Steps for printing star pattern

  1. Create a scanner class to get input from keyboard using new Scanner(System.in);
  2. accept integer value using scanner.nextInt() method
  3. Outer loop represents number of rows to print for (int row = 0; row < number; row++)
  4. Inner loop represents number of columns to print for (int col = 0; col < number; col++)
  5. Inside inner loop print patter System.out.print(" * ") this will print columns of * number of times.
  6. After closing inner loop print new line in outer loop to print again * columns in new line.
  7. This process will continue for user given number of times.


2 Square Star Pattern Program using While loop

Process to print star is same as printing using for loop.

Here we have to take care of initializing and incrementing loop variables, row and cols

  1. Outer loop variable row will initialize before outer loop and increment this after closing the inner loop and printing new line.
  2. Inner loop variable col must be initialize before inner loop and increment must be done inside inner loop.
  3. Print the * inside inner loop.


3 Square Star Pattern Program using Do While loop

Do while loop is similar to while loop.

only the condition is checked in last.


Insted of * you can use any other symbol like . $ @ etc

  1. Duplicate Number between 1 to n numbers
  2. Print vowels in a String
  3. Program to reverse a string
  4. Prime number program in java using while loop
  5. Print vowels in a String
  6. Sum of two numbers using command line arguments