ICSE Computer Science Java Programs | IT Developer
IT Developer

Java Programs - Solved 2025 ICSE Computer Science Paper



Share with a Friend

Solved 2025 ICSE Computer Science Paper

Class 10 - ICSE Computer Science Solved Papers

Sum of Row in Array Program - ICSE 2025 Computer Science

Question 6
Define a class to accept values into 4 × 4 array and find and display the sum of each row.


Example:
A[][] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {1, 3, 5, 7}, {2, 5, 3, 1}};


Output:
sum of row 1 = 10 (1 + 2 + 3 + 4)
sum of row 2 = 26 (5 + 6 + 7 + 8)
sum of row 3 = 16 (1 + 3 + 5 + 7)
sum of row 4 = 11 (2 + 5 + 3 + 1)

import java.util.Scanner; class Matrix{ public static void main(String[] args){ Scanner in = new Scanner(System.in); int a[][] = new int[4][4]; System.out.println("Enter matrix elements:"); for(int i = 0; i < a.length; i++){ for(int j = 0; j < a.length; j++){ a[i][j] = Integer.parseInt(in.nextLine()); } } System.out.println("Original Matrix:"); for(int i = 0; i < a.length; i++){ for(int j = 0; j < a.length; j++){ System.out.print(a[i][j] + "\t"); } System.out.println(); } for(int i = 0; i < a.length; i++){ int sum = 0; for(int j = 0; j < a.length; j++){ sum += a[i][j]; } System.out.println("sum of row " + (i + 1) + " = " + sum); } } }

Output

 
OUTPUT 1:
Enter matrix elements:
4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Original Matrix:
4   1   2   3   
4   5   6   7   
8   9   10  11  
12  13  14  15  
sum of row 1 = 10
sum of row 2 = 22
sum of row 3 = 38
sum of row 4 = 54

OUTPUT 2:
Enter matrix elements:
1
2
3
4
5
6
7
8
1
3
4
7
2
5
3
1
Original Matrix:
1   2   3   4   
5   6   7   8   
1   3   4   7   
2   5   3   1   
sum of row 1 = 10
sum of row 2 = 26
sum of row 3 = 15
sum of row 4 = 11