4
Define a class to accept values into a 3x3 integer array and print the product of each row elements.
Example:
| 3 | 1 | 2 |
| 4 | 2 | 1 |
| 5 | 1 | 2 |
Output:
Row 0 – 6
Row 1 – 8
Row 2 – 10
Show SolutionHide Solution↓
import java.util.Scanner;
public class RowProduct {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[][] arr = new int[3][3];
System.out.println("Enter 9 integers for the 3x3 array:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
arr[i][j] = sc.nextInt();
}
}
for (int i = 0; i < 3; i++) {
int product = 1;
for (int j = 0; j < 3; j++) {
product *= arr[i][j];
}
System.out.println("Row " + i + " – " + product);
}
}
}
