In: Computer Science
Given two integers N1 and N2, display the given box number pattern of '1's with a '0' at the center (Square number patterns).
Input:
5
5
where:
import java.util.Scanner;
public class BoxPattern {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m = sc.nextInt(); //get number of rows
int n = sc.nextInt(); //get number of columns
printPattern(m, n); //call method printPattern()
}
public static void printPattern(int m, int n)
{
for(int i = 0; i<m; i++) //loop throwugh rows
{
for(int j = 0; j<n; j++) //loop through columns
{
if(i == 0 || i == m-1 || j == 0 || j == n-1) //if it is the first/last row/column
{
System.out.print(1+" "); //print 1
}
else
{
System.out.print(0 + " "); //print 0
}
}
System.out.println(); //change line
}
}
}
Output below:
4
4
1 1 1 1
1 0 0 1
1 0 0 1
1 1 1 1
Kindly upvote if this version is correct.