Java Program To Print Boundary Elements

In this tutorial, we will be Going to Solve The Problem on Java Program To Print Boundary Elements of a Matrix. We will be looking at what are Matrices and Also how to display particularly The boundary elements.

Therefore, If you don’t Know About Matrices and How To check the boundary, then this is the perfect place to learn and solve the problem and Have a grip on it.

What is the Matrix?

A Matrice is a 2 -Dimensional Array with its rows and columns arrangements of its Elements. A matrix with m rows and n columns can be called an m*n matrix. Below is an example of a matrix:

For instance,int B[][[]=new int[m][n];


datatype varible[][]=new datatype [][];

Java Program to Print Boundary Elements of a matrix

Initially, we will be Taking Two Inputs which are No. of rows and Noo. of Columns of the matrix. Let them be’ and ‘n’ respectively.

Since we know that Input can be taken in two ways, one is through Scanner class object and another is discussed below Through Buffer Class object.

on the other hand, proceeding Forward, create The object of Buffer class and Take Inputs of m and n respectively. Create a 2-D array as discussed Above of type ‘int’.

Now comes by the loop execution, Take inputs of All the Matrix Elements, i.e,m*n elements are to be taken as input. For that, Run two loops to read all the elements of the matrix. Read and store them in a 2-D array.

Moreover, Now Comes The major Role of how to identify the boundary Elements in a Matrix. As we know, boundary elements Can Be First row, First Column, Last Row and Last Column.

Now, inside the For loop, to sum up, by Checking the above condition, we can Pick up boundary elements and Display Accordingly.

public class Array2
  public static void main(String args[])throws IOException
    int i,j,m,n;
    BufferedReader br=new BufferedReader(new InputStreamReader(;
    System.out.print("Enter the rows : "); 
    System.out.print("Enter the columns : "); 
    int Arr[][]=new int[m][n]; 
        System.out.println("Enter the elements : ");

    System.out.println("The Boundary Elements of Matrix are:");
        if(i==0 || j==0 || i == m-1 || j == n-1) 
          System.out.print(" \t");

In conclusion, Below is the output of the above Program displaying the boundary elements:

Print Boundary Elements of a matrix in Java

Also read:

Leave a Reply

Your email address will not be published. Required fields are marked *