ISC Practicals and Theory - Boundary Elements
Updated 21 Oct 2026
The boundary elements of a matrix are the elements that lie along its outer edge. This program, written in the main method alone, reads a matrix of order m x n, prints it, then prints its boundary elements and their sum.
Definition
In a two-dimensional array a with m rows and n columns, the element a[i][j] is a boundary element if it is in the first row, the last row, the first column or the last column. That is, if
i == 0 || i == m - 1 || j == 0 || j == n - 1
Every other element is a non-boundary (inner) element. Each boundary element is counted only once, even the four corners, which belong to a row and a column.
For example, in a $3 \times 4$ matrix with rows 1 2 3 4, 5 6 7 8 and 9 10 11 12, only 6 and 7 are inside. The boundary elements are 1 2 3 4 5 8 9 10 11 12 and their sum is $78 - 13 = 65$.
The program accepts an order from 2 to 10 for both rows and columns.
Algorithm
- Read the number of rows
mand columnsn. If either is not from 2 to 10, print an error message and stop. - Create the array
aof orderm x n, setsum = 0and read the elements row by row. - Print the matrix, a tab after each element and a new line after each row.
- Go through every element
a[i][j]. It is a boundary element when it is in the first row (i == 0), the last row (i == m - 1), the first column (j == 0) or the last column (j == n - 1). Print it and add it tosum. All four conditions are tested in oneif, so a corner element is printed and added only once. - Print the sum of the boundary elements.
Program
Everything is written inside main: the input, the display of the matrix and the loop with the four-way test for the boundary.
import java.util.Scanner;
/* Program BoundaryElements reads a matrix and displays its
boundary elements (first row, last row, first column, last column)
and the sum of those elements. */
public class BoundaryElements
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows and columns (2 to 10): ");
int m = sc.nextInt(); // number of rows of the matrix
int n = sc.nextInt(); // number of columns of the matrix
if (m < 2 || m > 10 || n < 2 || n > 10) // range check on the order
{
System.out.println("Invalid order. Rows and columns must be between 2 and 10.");
}
else
{
int a[][] = new int[m][n]; // stores the elements of the matrix
int sum = 0; // stores the sum of the boundary elements
System.out.println("Enter the elements:");
for (int i = 0; i < m; i++) // i moves over the rows
{
for (int j = 0; j < n; j++) // j moves over the columns
{
a[i][j] = sc.nextInt();
}
}
System.out.println("The matrix is:");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(a[i][j] + "\t");
}
System.out.println(); // new line after each row
}
// an element is on the boundary when it is in the first or last row or in the first or last column
System.out.println("Boundary elements:");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
if (i == 0 || i == m - 1 || j == 0 || j == n - 1)
{
System.out.print(a[i][j] + " ");
sum = sum + a[i][j]; // add to the running sum
}
}
}
System.out.println();
System.out.println("Sum of boundary elements = " + sum);
}
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
args | String[] | Command line arguments of main; not used |
sc | Scanner | Reads the numbers typed by the user |
m | int | Number of rows of the matrix |
n | int | Number of columns of the matrix |
a | int[][] | Elements of the matrix |
sum | int | Sum of the boundary elements |
i | int | Loop counter over the rows |
j | int | Loop counter over the columns |
Sample input and output
Sample 1
Enter the number of rows and columns (2 to 10): 3 4
Enter the elements:
1 2 3 4
5 6 7 8
9 10 11 12
The matrix is:
1 2 3 4
5 6 7 8
9 10 11 12
Boundary elements:
1 2 3 4 5 8 9 10 11 12
Sum of boundary elements = 65Sample 2
Enter the number of rows and columns (2 to 10): 2 2
Enter the elements:
-1 -2
-3 -4
The matrix is:
-1 -2
-3 -4
Boundary elements:
-1 -2 -3 -4
Sum of boundary elements = -10Sample 3
Enter the number of rows and columns (2 to 10): 11 3
Invalid order. Rows and columns must be between 2 and 10.Sample 4
Enter the number of rows and columns (2 to 10): 4 4
Enter the elements:
5 -3 8 2
7 1 9 4
6 0 -2 3
10 5 1 -6
The matrix is:
5 -3 8 2
7 1 9 4
6 0 -2 3
10 5 1 -6
Boundary elements:
5 -3 8 2 7 4 6 3 10 5 1 -6
Sum of boundary elements = 42Dry run
Take the first sample: $m = 3$, $n = 4$ with rows 1 2 3 4, 5 6 7 8, 9 10 11 12. sum is 0 at the start.
| i | j | Boundary? | Printed | sum |
|---|---|---|---|---|
| 0 | 0 to 3 | Yes (i == 0) | 1 2 3 4 | 10 |
| 1 | 0 | Yes (j == 0) | 5 | 15 |
| 1 | 1 | No | - | 15 |
| 1 | 2 | No | - | 15 |
| 1 | 3 | Yes (j == n - 1) | 8 | 23 |
| 2 | 0 to 3 | Yes (i == m - 1) | 9 10 11 12 | 65 |
The output is 1 2 3 4 5 8 9 10 11 12 and Sum of boundary elements = 65, the same as the first sample run.
Exam tip: Test all four conditions in one if inside the nested loops so that corner elements are printed and added only once.
Variations
- Non-boundary elements "Accept a matrix of order m x n and display the elements that are not on the boundary, with their sum."
- Diagonal sums "Find the sum of the left diagonal and the right diagonal elements of a square matrix."
- Border display "Display a square matrix with only the boundary elements shown and the inner positions left blank."