Write a Java program that calculates the sum of each row in a 2D array and prints the result.
Given the following array:
int[][] arr = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
Single Choice
Views 40
Answer:
public class Main { public static void main(String[] args) { int[][] arr = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; for (int i = 0; i < arr.length; i++) { int rowSum = 0; for (int j = 0; j < arr[i].length; j++) { rowSum += arr[i][j]; } System.out.println("Row " + (i+1) + " sum: " + rowSum); } } }
Related Articles:
This section is dedicated exclusively to Questions & Answers. For an in-depth exploration of Java Programming Language, click the links and dive deeper into this subject.
Join Our telegram group to ask Questions
Click below button to join our groups.