Boundary Sum of Matrix

This program calculates the sum of boundary elements of a matrix.

Problem Statement

Write a Java program to calculate the sum of boundary elements of a 4x4 matrix.

Source Code

1 public class MatrixBoundarySum {
2 public static void main(String[] args) {
3 System.out.println("Eduinq Matrix Boundary Sum");
4 int[][] A = {
5 {1,2,3,4},
6 {5,6,7,8},
7 {9,10,11,12},
8 {13,14,15,16}
9 };
10 int sum=0;
11 for(int i=0;i<4;i++){
12 for(int j=0;j<4;j++){
13 if(i==0||i==3||j==0||j==3) sum+=A[i][j];
14 }
15 }
16 System.out.println("Boundary sum: "+sum);
17 }
18 }

Program Output

Eduinq Matrix Boundary Sum
Boundary sum: 102

Explanation

Boundary elements are those in the first/last row or column, and the condition checks these positions while sum accumulates matching values, demonstrating boundary sum calculation.

Quick Links to Explore