0% found this document useful (0 votes)
636 views2 pages

Java Program To Print Boundary Elements of A 2D Array

This program takes in the dimensions of a 2D array from the user, inputs values into the array, and prints out the boundary elements. It creates a 2D integer array based on the user-input dimensions, uses nested for loops to input values into the array and check for boundary elements, which are printed separately from non-boundary elements. The program handles both rectangular and square matrices by conditionally replacing row and column variables with a single dimension value.

Uploaded by

AtharvaShukla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
636 views2 pages

Java Program To Print Boundary Elements of A 2D Array

This program takes in the dimensions of a 2D array from the user, inputs values into the array, and prints out the boundary elements. It creates a 2D integer array based on the user-input dimensions, uses nested for loops to input values into the array and check for boundary elements, which are printed separately from non-boundary elements. The program handles both rectangular and square matrices by conditionally replacing row and column variables with a single dimension value.

Uploaded by

AtharvaShukla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java Program to print Boundary

Elements of a 2D Array
Question:
Write a Program in Java to input a 2-D array of size r*c and print its boundary (border) elements.
For example:

Programming Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* 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,r,c;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

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

int A[][]=new int[r]1; //Creating the array
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

/* Inputting the array */
for(i=0;i<r;i++)
{
for(j=0;j<c;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<r;i++)
{
for(j=0;j<c;j++)
{
if(i==0 || j==0 || i == r-1 || j == c-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 r and c in the above program with n.


Source: http://www.javaforschool.com/1526935-java-program-to-print-boundary-
elements/#ixzz3CbPFKY4A

You might also like