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
- Create a scanner class to get input from keyboard using
new Scanner(System.in);
- accept integer value using
scanner.nextInt()
method - Outer loop represents number of rows to print
for (int row = 0; row < number; row++)
- Inner loop represents number of columns to print
for (int col = 0; col < number; col++)
- Inside inner loop print patter
System.out.print(" * ")
this will print columns of * number of times. - After closing inner loop print new line in outer loop to print again * columns in new line.
- This process will continue for
user given number
of times.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import java.util.Scanner; public class StarPattern { public static void main(String[] s) { System.out.println("Enter rows to print square pattern"); Scanner scanner = new Scanner(System.in); int number = scanner.nextInt(); for (int row = 0; row < number; row++) { for (int col = 0; col < number; col++) { System.out.print(" * "); } System.out.println(""); } } } |
Output
1 2 3 4 5 6 7 8 | Enter rows to print square pattern 5 * * * * * * * * * * * * * * * * * * * * * * * * * |
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
- Outer loop variable row will initialize before outer loop and increment this after closing the inner loop and printing new line.
- Inner loop variable col must be initialize before inner loop and increment must be done inside inner loop.
- Print the
*
inside inner loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import java.util.Scanner; public class StarPattern1 { public static void main(String[] s) { System.out.println("Enter rows to print square pattern"); Scanner scanner = new Scanner(System.in); int number = scanner.nextInt(); int row = 0; while (row < number) { int col = 0; while (col < number) { System.out.print(" * "); col++; } System.out.println(""); row++; } } } |
Output
1 2 3 4 5 6 7 8 | Enter rows to print square pattern 5 * * * * * * * * * * * * * * * * * * * * * * * * * |
3 Square Star Pattern Program using Do While loop
Do while loop is similar to while loop.
only the condition is checked in last.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import java.util.Scanner; public class StarPattern2 { public static void main(String[] s) { System.out.println("Enter rows to print square pattern"); Scanner scanner = new Scanner(System.in); int number = scanner.nextInt(); int i = 0; do { int j = 0; do { System.out.print(" * "); j++; } while (j < number); System.out.println(""); i++; } while (i < number); } } |
Output
1 2 3 4 5 6 7 8 | Enter rows to print square pattern 5 * * * * * * * * * * * * * * * * * * * * * * * * * |
Insted of * you can use any other symbol like . $ @
etc