Java Program to Print Hollow Box Number Pattern

Write a Java Program to Print Hollow Box Number Pattern using For Loop and While Loop with example.

Java Program to Print Hollow Box Number Pattern using For Loop

This Java program allows the user to enter the number of rows and column values. Next, it prints the Hollow Box number pattern of 1’s. I mean, it prints First row, last row, first column, and last column as 1’s, and the remaining elements as Empty.

// Java Program to Print Hollow Box Number Pattern
import java.util.Scanner;

public class HollowBoxNumber1 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int rows, columns, i, j;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter Number of Rows : ");
		rows = sc.nextInt();	
		
		System.out.print(" Please Enter Number of Columns : ");
		columns = sc.nextInt();	
		
		for(i = 1; i <= rows; i++)
		{
			for(j = 1; j <= columns; j++)
			{
				if(i == 1 || i == rows || j == 1 || j == columns)
				{
					System.out.print("1"); 
				}
				else
				{
					System.out.print(" "); 
				}
			}
			System.out.print("\n"); 
		}	
	}
}
Java Program to Print Hollow Box Number Pattern 1

The first For loop is to iterate from 1 to the total number of rows. Next, we used Nested For Loop to iterate j from 1 to the total number of columns.

User entered value: rows  = 7, and columns = 9

First For Loop – First Iteration: for(i = 1; i <= 7; i++)
Condition is True. So, it enters into second For Loop

Second For Loop – First Iteration: for(j = 1; 1 <= 9; 1++)
Condition is True. So, it checks the condition inside the If statement
if(i == 1 || i == rows || j == 1 || j == columns)
=> if(1 == 1 || 1 == 7 || 1 == 1 || 1 == 9) – Condition is True. So, it will print 1

Repeat the same for remaining Java iteration.

Program to Print Hollow Box Number Pattern using While Loop

This Java program to display the hollow box of numbers is the same as the above example, but we are using the While Loop.

// Java Program to Print Hollow Box Number Pattern
import java.util.Scanner;

public class HollowBoxNumber2 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int rows, columns, i = 1, j;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter Number of Rows : ");
		rows = sc.nextInt();	
		
		System.out.print(" Please Enter Number of Columns : ");
		columns = sc.nextInt();	
		
		while(i <= rows)
		{
			j = 1;
			while(j <= columns)
			{
				if(i == 1 || i == rows || j == 1 || j == columns)
				{
					System.out.print("1 "); 
				}
				else
				{
					System.out.print("  "); 
				}
				j++;
			}
			i++;
			System.out.print("\n"); 
		}	
	}
}

Java Hollow Box Number Pattern using a While Loop output

 Please Enter Number of Rows : 9
 Please Enter Number of Columns : 14
1 1 1 1 1 1 1 1 1 1 1 1 1 1 
1                         1 
1                         1 
1                         1 
1                         1 
1                         1 
1                         1 
1                         1 
1 1 1 1 1 1 1 1 1 1 1 1 1 1 

Java Program to Print Hollow Box Number Pattern Example 3

This Java program is the same as the first example. But it prints the first row, last row, first column, and last column as 0’s, and the remaining elements as empty space.

// Java Program to Print Hollow Box Number Pattern
import java.util.Scanner;

public class HollowBoxNumber3 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int rows, columns, i, j;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter Number of Rows : ");
		rows = sc.nextInt();	
		
		System.out.print(" Please Enter Number of Columns : ");
		columns = sc.nextInt();	
		
		for(i = 1; i <= rows; i++)
		{
			for(j = 1; j <= columns; j++)
			{
				if(i == 1 || i == rows || j == 1 || j == columns)
				{
					System.out.print("0 "); 
				}
				else
				{
					System.out.print("  "); 
				}
			}
			System.out.print("\n"); 
		}	
	}
}
 Please Enter Number of Rows : 10
 Please Enter Number of Columns : 20
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0                                     0 
0                                     0 
0                                     0 
0                                     0 
0                                     0 
0                                     0 
0                                     0 
0                                     0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 

Comments are closed.