Project Solution: Hollow Square Pattern
☰Fullscreen
Table of Content:
Solution: HollowSquare.java
import java.util.Scanner; public class HollowSquare { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n; System.out.print("Enter the number of rows: "); n = scanner.nextInt(); for (int i = n; i >= 1; i--) { for (int j = 1; j <= i - 1; j++) { System.out.print(" "); } for (int k = 1; k <= n; k++) { if (i == n || i == 1 || k == 1 || k == n) { System.out.print("*"); } else { System.out.print(" "); } } System.out.println(); } scanner.close(); } }
Enter the number of rows: 5 ***** * * * * * * *****
inside star
import java.util.Scanner; public class HollowSquare { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n; System.out.print("Enter the number of rows: "); n = scanner.nextInt(); for (int i=n;i>=1;i--) { for (int j=1;j<=i-1;j++) { System.out.print(" "); } for(int k=1;k<=n;k++) { System.out.print("*"); } System.out.print("\n"); } } }
Output:
Enter the number of rows: 5 ***** ***** ***** ***** *****