Tuesday , 17 January 2017
Home » Array Related Programs » Java Program to print Boundary Elements of a 2D Array

Java Program to print Boundary Elements of a 2D Array

Question:

Write a Program in Java to input a 2-D array of size ‘m*n’ and print its boundary (border) elements.

For example:

boundary elements

Programming Code:

/**
* The class Boundary_Element accesses and prints the boundary elements of a 2D array
* @author : www.javaforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.io.*;
class Boundary_Element
{
    public static void main(String args[])throws IOException
    {
        int i,j,m,n;
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

        System.out.print("Enter the no. of  rows: "); //Inputting the number of rows
        m=Integer.parseInt(br.readLine());
        System.out.print("Enter the no. of columns: "); //Inputting the number of columns
        n=Integer.parseInt(br.readLine());

        int A[][]=new int[m][n]; //Creating the array
        
        /* Inputting the array */
        for(i=0;i<m;i++)
        {
            for(j=0;j<n;j++)
            {
                System.out.print("Enter the elements: ");
                A[i][j]=Integer.parseInt(br.readLine());
            }
        }
        
        System.out.println("The Boundary Elements are:");
        for(i=0;i<m;i++)
        {
            for(j=0;j<n;j++)
            {
                if(i==0 || j==0 || i == m-1 || j == n-1) //condition for accessing boundary elements
                    System.out.print(A[i][j]+"\t");
                else
                    System.out.print(" \t");
            }
            System.out.println();
        }
    }
}

Note: If you are asked to input a square matrix of size ‘n*n’ then just input the value of ‘n’ and replace ‘m’ and ‘n’ in the above program with ‘n’.

Output:

boundary elements output

Check Also

ISC 2016 Computer Science Solution + Examiner’s Comments – From the Council

Solution of ISC 2016 Computer science Paper as provided by the Council for the Indian School Certificate Examinations.

8 comments

  1. Chirag Pandey Bose

    what is the function of “t”? Thanks.

  2. its really helpfull….thanx a lot….dude….

  3. Sir, Can you give the method for filling an array with a particular type of number ? I am stuck at that.

  4. Thankyou soo much maam/sir fr this program..ur blog is really really veryy helpful i have prepared fr my boards practical only with the help of it thankyou 🙂

  5. plz tell what is the condition for the validation of a date???

    if condition is given,

    if(d>0&&d<=month[m] && m>0&&m<=12 && y>0 && y<=9999)

     

    then while random checking,if d and y doesn’t satisfy condition,it returns invalid input,while if month is not satisfied,it returns array index out of bounds exception… as in the case of ur posts… future date…

     

    so plz go through and solve this problem of mine…

Leave a Reply to Chirag Pandey Bose Cancel reply

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