Sum of Each Row of A Matrix

Sum of Each Row of A Matrix:


Program:

import java.util.*;

public class MatrixRowSum {

public static void main(String[] args) {

int r, c;

Scanner s = new Scanner(System.in);

r = s.nextInt();

c = s.nextInt();

int[][] matrix = new int[r][c];

for(int i = 0; i<r; i++) {

for(int j = 0; j<c;j++) {

matrix[i][j] = s.nextInt();

}

}

int sum = 0;

for(int i = 0; i<r; i++) {

sum = 0;

for(int j = 0;j<c; j++) {

sum += matrix[i][j];

}

System.out.println(i+1 + "  " + "Row sum is: " + ""+ sum);

}

}

}


Output:

1)

3 3

1 2 3 4 5 6 7 8 9

1  Row sum is: 6

2  Row sum is: 15

3  Row sum is: 24

2)

3 4

1 2 3 4 5 6 7 8 9 10 11 12

1  Row sum is: 10

2  Row sum is: 26

3  Row sum is: 42


                                                                         Happy Coding 💻 :-)



Comments

Popular posts from this blog

Valid Username Regular Expression

Rotate String in Right Direction in java

Sum of Elements of Lower Triangular Matrix