Matrix Prime Sums | ISC Computer Science Sample Paper 2026

Write a program to declare a matrix A[][] of order (M × N) where ‘M’ is the number of rows and ‘N’ is the number of columns such that both M and N must be greater than 2 and less than 10. Allow the user to input positive integers into this matrix. Display appropriate error message for an invalid input.

Perform the following tasks on the matrix:
(a) Display the input matrix.
(b) Compute the sum of prime elements for each row and column.
(c) Identify the row index with maximum prime-sum and the column index with maximum prime-sum.
(d) Print both indices and sums. If no primes found in all, print a message.

Example:
INPUT: M = 3 and N = 4 and array is:

4   5   6   7
8 11 10 13
1 2 3 4

then, OUTPUT:
Row with max prime-sum: 1 (sum = 24)
Column with max prime-sum: 3 (sum = 20)

Test your program for the following data and some random data:

Example 1
INPUT:
M = 4
N = 4
Array elements: 11, 3, 4, 7, 13, 9, 5, 17, 2, 3, 19, 23, 12, 6, 13, 4
OUTPUT:
Original array:

11  3   4   7
13 9 5 17
2 3 19 23
12 6 13 4

Row with max prime-sum: 2 (sum = 47)
Column with max prime-sum: 3 (sum = 47)

Example 2
INPUT:
M = 3
N = 3
Array elements: 2, 6, 4, 11, 5, 9, 12, 7, 13
OUTPUT:
Original array:

 2  6   4
11 5 9
12 7 13

Row with max prime-sum: 2 (sum = 20)
Column with max prime-sum: 0 and 2 (sum = 13)

Example 3
INPUT:
M = 3
N = 3
Array elements: 12, 11, -5, 9, 1, 7, 13, 2, 3
OUTPUT: INVALID INPUT

Example 4
INPUT:
M = 2
N = 3
OUTPUT: INVALID INPUT

import java.util.Scanner;
class PrimeSums{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("M = ");
        int M = in.nextInt();
        System.out.print("N = ");
        int N = in.nextInt();
        if(M < 3 || M > 9 || N < 3 || N > 9){
            System.out.println("INVALID INPUT");
            return;
        }
        int A[][] = new int[M][N];
        System.out.println("Array elements:");
        for(int i = 0; i < M; i++){
            for(int j = 0; j < N; j++){
                A[i][j] = in.nextInt();
                if(A[i][j] < 0){
                    System.out.println("INVALID INPUT");
                    return;
                }
            }
        }
        System.out.println("Original array:");
        int maxRow = 0;
        String rowIndex = "";
        for(int i = 0; i < M; i++){
            int sum = 0;
            for(int j = 0; j < N; j++){
                System.out.print(A[i][j] + "\t");
                if(isPrime(A[i][j]))
                    sum += A[i][j];
            }
            System.out.println();
            if(maxRow < sum){
                maxRow = sum;
                rowIndex = i + "";
            }
            else if(maxRow == sum)
                rowIndex += " " + i;
        }
        if(maxRow == 0)
            System.out.println("NO PRIME NUMBERS FOUND IN ANY ROW");
        else
            System.out.println("Row with max prime-sum: " + rowIndex + " (sum = " + maxRow + ")");
        int maxColumn = 0;
        String colIndex = "";
        for(int i = 0; i < N; i++){
            int sum = 0;
            for(int j = 0; j < M; j++){
                if(isPrime(A[j][i]))
                    sum += A[j][i];
            }
            if(maxColumn < sum){
                maxColumn = sum;
                colIndex = i + "";
            }
            else if(maxColumn == sum)
                colIndex += " " + i;
        }
        if(maxColumn == 0)
            System.out.println("NO PRIME NUMBERS FOUND IN ANY COLUMN");
        else
            System.out.println("Column with max prime-sum: " + colIndex + " (sum = " + maxColumn + ")");
    }
    public static boolean isPrime(int num){
        int f = 0;
        for(int i = 1; i <= num; i++){
            if(num % i == 0)
                f++;
        }
        return f == 2;
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *