-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRowWiseSum.java
50 lines (44 loc) · 1.07 KB
/
RowWiseSum.java
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
49
50
/*Row Wise Sum
For two-dimensional integer array/list of size (N x M), find and print the sum of each of the row elements in a single line, separated by a single space.*/
import java.util.Scanner;
public class RowWiseSum
{
public static void RowSum(int arr[][])
{
String str = "";
int rows=arr.length;
int cols=arr[0].length;
for(int i=0;i<rows;i++)
{
int sum =0;
for(int j=0;j<cols;j++)
{
sum += arr[i][j];
}
System.out.print(sum + " ");
}
}
public static int[][] takeInput()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of rows");
int rows=sc.nextInt();
System.out.println("Enter number of cols");
int cols=sc.nextInt();
int[][] arr=new int[rows][cols];
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
System.out.println("Enter the element at "+ i+ " row "+j+"column");
arr[i][j]=sc.nextInt();
}
}
return arr;
}
public static void main(String args[])
{
int [][]input=takeInput();
RowSum(input);
}
}