Given a 4×4 matrix the task is to sum boundary elements of the matrix.
Example: Consider the 4×4 matrix
| 3 | 1 | 4 | 6 |
| 8 | 1 | 9 | 2 |
| 2 | 7 | 3 | 5 |
| 4 | 2 | 1 | 6 |
Sum of boundary elements: 44
Java
import java.util.*;
class SumBoundaryElements
{
public static void main(String args[])
{
int sum=0;
Scanner sc=new Scanner(System.in);
int ar[][]=new int[4][4];
System.out.println("Enter matrix elements:-");
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
ar[i][j]=sc.nextInt();
}
}
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
if(i==0||i==3||j==0||j==3)
{
sum=sum+ar[i][j];
}
}
}
System.out.println("Sum of boundary elements: "+sum);
}
}Java