In this tutorial, we will write a program to print hollow rectangle pattern in java. Before that, you may go through the following topic in java.
The program takes a user input for the number of rows and columns for the hollow rectangle. Then using two for loops it will print the star rectangular pattern in java.
Example:
1 2 3 4 5 6 7 8 9 10 | <em>Inputs:</em> <em> Rows: 6 Columns:10</em> <strong>Output:</strong> <strong>********** * *</strong> <strong>* * * * * * **********</strong> |
Java Program to Print Rectangle Star Pattern
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | import java.util.Scanner; public class Main { public static void main(String args[]) { int rows, columns, i, j; Scanner sc = new Scanner(System.in); System.out.print("Enter the no. of rows: "); rows = sc.nextInt(); System.out.print("Enter the no. of columns: "); columns = sc.nextInt(); System.out.print("Rectangular star Pattern: \n\n"); for (i = 1; i <= rows; i++) { for (j = 1; j <= columns; j++) { if (i == 1 || i == rows || j == 1 || j == columns) System.out.print("*"); else System.out.print(" "); } System.out.println(); } } } |
Output: