Prashnikaप्रश्निका
Code
About
‹ All Computer Science notes

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

  1. Read the number of rows m and columns n. If either is not from 2 to 10, print an error message and stop.
  2. Create the array a of order m x n, set sum = 0 and read the elements row by row.
  3. Print the matrix, a tab after each element and a new line after each row.
  4. 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 to sum. All four conditions are tested in one if, so a corner element is printed and added only once.
  5. 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

VariableData TypePurpose
argsString[]Command line arguments of main; not used
scScannerReads the numbers typed by the user
mintNumber of rows of the matrix
nintNumber of columns of the matrix
aint[][]Elements of the matrix
sumintSum of the boundary elements
iintLoop counter over the rows
jintLoop 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 = 65

Sample 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 = -10

Sample 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 = 42

One value per line

Dry 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.

ijBoundary?Printedsum
00 to 3Yes (i == 0)1 2 3 410
10Yes (j == 0)515
11No-15
12No-15
13Yes (j == n - 1)823
20 to 3Yes (i == m - 1)9 10 11 1265

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

  1. Non-boundary elements "Accept a matrix of order m x n and display the elements that are not on the boundary, with their sum."
  2. Diagonal sums "Find the sum of the left diagonal and the right diagonal elements of a square matrix."
  3. Border display "Display a square matrix with only the boundary elements shown and the inner positions left blank."