ISC Practicals and Theory - Non-Boundary Elements
Updated 21 Oct 2026
The non-boundary elements of a matrix are the elements that are not on its outer edge. This program, written in the main method alone, reads a matrix of order m x n, prints it, then prints the inner elements and their sum.
Definition
In a two-dimensional array a with m rows and n columns, the element a[i][j] is a non-boundary (inner) element if it is not in the first row, the last row, the first column or the last column. That is, i runs from 1 to m - 2 and j runs from 1 to n - 2.
For example, in a $4 \times 4$ matrix with rows 5 -3 8 2, 7 1 9 4, 6 0 -2 3 and 10 5 1 -6, the inner elements are 1 9 0 -2 and their sum is $1 + 9 + 0 - 2 = 8$.
A matrix with only 2 rows or only 2 columns has no inner elements, because every element is in the first or last row or column. The program then prints No non-boundary elements and no sum.
The order must lie between 2 and 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.
- If
mornis 2, no row or column is left inside, so printNo non-boundary elements. - Otherwise run
ifrom 1 tom - 2andjfrom 1 ton - 2. This skips the first and last row and the first and last column without any extraif. Print eacha[i][j]and add it tosum. - Print the sum of the non-boundary elements.
Program
Everything is written inside main: the input, the display of the matrix and the loops that start at 1 and stop at m - 2 and n - 2.
import java.util.Scanner;
/* Program NonBoundaryElements reads a matrix and displays its
non-boundary (inner) elements and the sum of those elements. */
public class NonBoundaryElements
{
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 non-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
}
// the inner elements are those of rows 1 to m-2 and columns 1 to n-2;
// if m or n is 2, there are none and a message is printed
if (m == 2 || n == 2) // no row or no column is left inside
{
System.out.println("No non-boundary elements");
}
else
{
System.out.println("Non-boundary elements:");
for (int i = 1; i <= m - 2; i++) // skip first and last row
{
for (int j = 1; j <= n - 2; j++) // skip first and last column
{
System.out.print(a[i][j] + " ");
sum = sum + a[i][j]; // add to the running sum
}
}
System.out.println();
System.out.println("Sum of non-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 non-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): 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
Non-boundary elements:
1 9 0 -2
Sum of non-boundary elements = 8Sample 2
Enter the number of rows and columns (2 to 10): 3 3
Enter the elements:
4 5 6
7 -8 9
1 2 3
The matrix is:
4 5 6
7 -8 9
1 2 3
Non-boundary elements:
-8
Sum of non-boundary elements = -8Sample 3
Enter the number of rows and columns (2 to 10): 2 4
Enter the elements:
1 2 3 4
5 6 7 8
The matrix is:
1 2 3 4
5 6 7 8
No non-boundary elementsSample 4
Enter the number of rows and columns (2 to 10): 5 11
Invalid order. Rows and columns must be between 2 and 10.Dry run
Take the first sample: $m = 4$, $n = 4$ with rows 5 -3 8 2, 7 1 9 4, 6 0 -2 3, 10 5 1 -6. Since m and n are not 2, the loops run with i from 1 to 2 and j from 1 to 2. sum is 0 at the start.
| i | j | a[i][j] | sum |
|---|---|---|---|
| 1 | 1 | 1 | 1 |
| 1 | 2 | 9 | 10 |
| 2 | 1 | 0 | 10 |
| 2 | 2 | -2 | 8 |
The output is 1 9 0 -2 and Sum of non-boundary elements = 8, the same as the first sample run.
Exam tip: Start both loops at 1 and stop at m - 2 and n - 2; this skips the boundary without any extra if.
Variations
- Boundary elements "Accept a matrix of order m x n and display the boundary elements, with their sum."
- Diagonal sums "Find the sum of the left diagonal and the right diagonal elements of a square matrix."
- Inner display "Display a square matrix with only the inner elements shown and the boundary positions left blank."