How to print hollow rectangle star pattern in Java

1 Answer

0 votes
public class Program
{
	private static void print_hollow_rectangle(int total_stars) {
       for (int i = 1; i <= total_stars; i++) {
    	  for (int j = 1; j <= total_stars; j++) {
    		 if (i == 1 || i == total_stars || j == 1 || j == total_stars) {
    			System.out.print("*");
    		 }
    		 else {
    			System.out.print(" ");
    		 }
    	  }
    	  System.out.println();
       }
    }
	public static void main(String[] args)
	{
		print_hollow_rectangle(7);
	}
}





/*
run:
    
*******
*     *
*     *
*     *
*     *
*     *
*******
    
*/

 



answered Sep 11, 2023 by avibootz
...