1. Write an application that prompts the user to enter the size of the side of a square, and then displays a hollow square of that size made of asterisks. Your program should work for squares of all side lengths between 1 and 20. This is a sample run of your program: Enter·the·size·of·the·asterisk·square·from·1-20:3↵ ***↵ *·*↵ ***↵

Answer :

Answer:

import java.util.Scanner;

public class HollowSquare

{

public static void main(String args[])

{

Scanner scan = new Scanner(System.in);

int size;

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

size = scan.nextInt();

if(size>=1 && size<=20)

{

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

{

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

{

if(i==0 || j==0 || i==size-1 || j==size-1)

System.out.print("*");

else

System.out.print(" ");

}

System.out.println();

}

}

else

System.out.println("Invalid size.");

}

}

Other Questions