Square Star Pattern

Program to Print Square Star Pattern

PRINTING PATTERN:

****

****

****

****
            

PREREQUISITE:

Basic knowledge of Java language and loops.

ALGORITHM:

1. Take the number of rows as input from the user (length of the square's side) and store it in a variable ('l').
2. Run a loop 'l' times to iterate through the rows (i=0 to i<l).
3. Run a nested loop inside the previous loop to iterate through the columns (j=0 to j<l).
4. Print ‘*’ inside the nested loop to display stars in all columns of a row.
5. Move to the next line by printing a new line (System.out.println()).

Code in Java:

import java.util.Scanner;

public class SquarePattern {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the number of rows/columns");
        int l = scanner.nextInt(); // Taking input for number of rows
        
        for (int i = 0; i < l; i++) {  // Outer loop for number of rows  
            for (int j = 0; j < l; j++) {  // Inner loop for number of columns in each row
                System.out.print("*");  // Printing '*' in each column of a row.
            }
            System.out.println();  // Printing a new line after each row has been printed.
        }
        scanner.close();
    }
}
            

Output:

Enter the number of rows/columns
4
****
****
****
****