-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSumof_eachRow_eachColumn.java
More file actions
48 lines (41 loc) · 1.33 KB
/
Copy pathSumof_eachRow_eachColumn.java
File metadata and controls
48 lines (41 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package Examples;
import java.util.Scanner;
public class Sumof_eachRow_eachColumn {
public static void main(String args[])
{
int i, j , sumrow, sumcol;
Scanner inp = new Scanner(System.in);
System.out.print("Enter total rows: ");
int row=inp.nextInt();
System.out.print("Enter total columns: ");
int col=inp.nextInt();
int [][] arr = new int[row][col];
System.out.println("Enter matrix:");
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
arr[i][j] = inp.nextInt();
}
System.out.println("Orignal Matrix: ");
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
System.out.print(arr[i][j]+" ");
System.out.println();
}
for(i=0; i<row; i++) // fixing rows i.e. for row 0 column will move 0, 1, 2,...
{
sumrow = 0;
for(j=0; j<col; j++) // fixing row and moving columns
sumrow += arr[i][j]; // 0,0 , 0,1 , 0,2 for each row, after 1 row it will go to the next row after printing row1 values
System.out.println("Sum of " + (i+1) +" row: " + sumrow);
}
for(i=0; i<col; i++) // fixing columns i.e. for column 0 row will move 0, 1, 2,...
{
sumcol = 0;
for(j=0; j<row; j++) // fixing column and moving rows
sumcol += arr[j][i]; // 0,0 , 1,0 , 2,0 its a column
System.out.println("Sum of " + (i+1) +" column: " + sumcol);
}
}
}