asked 177k views
4 votes
Write a program to create an array of size m X n and print the sum of all the numbers row wise in java

asked
User Wrdieter
by
8.5k points

1 Answer

4 votes

Answer:

Here is the program to create an array of size m X n and print the sum of all the numbers row wise in Java:

```

import java.util.Arrays;

public class ArraySumRowWise {

public static void main(String[] args) {

int m = 3;

int n = 4;

int[][] arr = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};

// Print the original array

System.out.println("Original Array:");

for (int[] row : arr) {

System.out.println(Arrays.toString(row));

}

// Compute and print the row wise sum

System.out.println("Row Wise Sum:");

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

int rowSum = 0;

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

rowSum += arr[i][j];

}

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

}

}

}

```

Step-by-step explanation:

In this program, we have created an array of size 3 X 4 and initialized it with some values. We have then printed the original array and computed the row wise sum by iterating over each row and adding up all the elements. Finally, we have printed the row wise sums. You can replace the values of m, n, and arr with your own values to test the program with different array sizes and values.

answered
User JeremyD
by
8.1k points

No related questions found