Answer:
Step-by-step explanation:
import java.util.Scanner;
public class SquareDisplay {
public static void main(String []args){
// Scanner is used to get value from command line
Scanner console = new Scanner(System.in);
System.out.print("Enter an integer in the range of 1-15:");
int number = console.nextInt();
// check if number is greater then 15 or less then 1 then return error message
if(number>0 && number<=15){
System.out.println("Error :Wrong Input");
return;
}
for( int i=0; i< number; i++){
for( int j =0; j<number; j++){
System.out.print("X");
}
System.out.print(" ");
}
}
}
Code Explanation
As we want to show the square of X's we need to execute 2 nested for loop to achieve the target result. Outer for loop will determine that how many times we need to show the group of X's.
And the inner for loop will actually display's number of X's for every outer loop iteration.
Output
Case 1:
Enter an integer in the range of 1-15:5
XXXXX XXXXX XXXXX XXXXX XXXXX
Case 2:
Enter an integer in the range of 1-15:16
Error :Wrong Input
Case 3:
Enter an integer in the range of 1-15:2
XX XX