{{#items}}

{{text}}

{{/items}} {{ctaText}}

{{#items}} {{/items}}

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}
```

similar questions

how to print elements in matrix javaadding elements in a specified column or row in a two dimensional array javaqueries leading to this page

sum of boundary elements of matrix in javasum of boundary elements of matrix in java your answer for

you will get a confirmation link on this - you will have to click that for successful submission of your answer. we require this to keep the website free of spam, bots and unhelpful content

please ensure to add code which is syntactically corrent and executes properly

ask question on maxinterview

you will get a confirmation link on this - you will have to click that for successful submission of your question. we require this to keep the website free of spam, bots and unhelpful content

please be clear, to the point and respectful