asked 122k views
5 votes
Create a Java program called Square which uses a nested for loop to create a square shape. The ogram prompts the user to enter the size of the square; e.g. if the user enters 5 then lines of stars will output to screen forming a square shape which is 5 lines in length and 5 lines in breadth



1 Answer

2 votes

Answer:

import java.util.Scanner;

public class Square {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the size of the square: ");

int size = scanner.nextInt();

scanner.close();

for (int i = 0; i < size; i++) {

for (int j = 0; j < size; j++) {

System.out.print("*");

}

System.out.println();

}

}

}

Step-by-step explanation: