Given a matrix of custom size the task is to sum the non-boundary elements.
Example: Consider a matrix:-
| 11 | 4 | 5 | 7 |
| 3 | 10 | 12 | 13 |
| 5 | 6 | 11 | 9 |
Here sum non-boundary elements is 10+12 = 22.
The program should output only the sum of non-boundary elements.
Java
import java.util.*;
public class SumNonBoundaryElements
{
public static void main(String args[])
{
int row=0,col=0,sum=0;
Scanner sc=new Scanner(System.in);
System.out.print("Enter no.of rows: ");
row=sc.nextInt();
System.out.print("Enter no.of columns: ");
col=sc.nextInt();
int ar[][]=new int[row][col];
System.out.println("Enter matrix elements:-");
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
ar[i][j]=sc.nextInt();
}
}
System.out.print("Boundary elements are: ");
for(int i=1;i<row-1;i++)
{
for(int j=1;j<col-1;j++)
{
sum=sum+ar[i][j];
}
}
System.out.println("Sum of non-boundary elements is "+sum);
}
}Java