Java Program to Print Box Number Pattern of 1 and 0

Write a Java Program to Print Box Number Pattern of 1 and 0 using For Loop, and While Loop with example.

Java Program to Print Box Number Pattern of 1 and 0 using For Loop

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

// Java Program to Print Box Number Pattern of 1 and 0
import java.util.Scanner;

public class BoxNumber1 {
	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("0"); 
				}
			}
			System.out.print("\n"); 
		}	
	}
}
Java Program to Print Box Number Pattern of 1 and 0 1

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  = 10, and columns = 15

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

Second For Loop – First Iteration: for(j = 1; 1 <= 15; 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 == 10 || 1 == 1 || 1 == 15) – Condition is True so, it prints 1

Repeat the same for remaining Java iteration.

Java Program to Print Box Number Pattern of 1 and 0 using While Loop

This program is the same as the above example. But in this Java program, we are using the While Loop to display box number pattern of 1 and 0.

// Java Program to Print Box Number Pattern of 1 and 0
import java.util.Scanner;

public class BoxNumber2 {
	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("0 "); 
				}
				j++;
			}
			i++;
			System.out.print("\n"); 
		}	
	}
}
 Please Enter Number of Rows : 7
 Please Enter Number of Columns : 9
1 1 1 1 1 1 1 1 1 
1 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 1 
1 1 1 1 1 1 1 1 1 

Program to Print Box Number Pattern of 0 and 1 Example 3

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

// Java Program to Print Box Number Pattern of 0 and 1
import java.util.Scanner;

public class BoxNumber3 {
	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("1 "); 
				}
			}
			System.out.print("\n"); 
		}	
	}
}
 Please Enter Number of Rows : 8
 Please Enter Number of Columns : 14
0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0