sum of boundary elements of matrix in java

Solutions on MaxInterview for sum of boundary elements of matrix in java by the best coders in the world

showing results for - "sum of boundary elements of matrix in java"
Paulina
05 Mar 2020
1import java.util.*;
2public class Main
3{
4	public static void main(String[] args)
5	{
6		Scanner sc = new Scanner(System.in);
7		int m, n, sum = 0, row1 = 0, col_n = 0, diag = 0;
8		System.out.print(“\nEnter the order of the matrix : “);
9		m = sc.nextInt();
10		n = sc.nextInt();
11		int i, j;
12		int[][] mat = new int[m][n];
13		System.out.print(“\nInput the matrix elements \n”);
14		for(i = 0; i < m; i++)
15			{
16				for(j = 0; j < n; j++)
17				mat[i][j] = sc.nextInt();
18			}
19		System.out.print(“\nBoundary Matrix\n”);
20		for(i = 0; i < m; i++)
21			{
22				for(j = 0; j < n; j++)
23					{
24						if (i == 0 || j == 0 || i == n – 1 || j == n – 1)
25							{
26								System.out.print(mat[i][j] + ” “);
27								sum = sum + mat[i][j];
28							}
29						else
30								System.out.print(” “);
31					}
32				System.out.print(“\n”);
33			}
34		System.out.print(“\nSum of boundary is ” + sum);
35	}
36}