Java Program to Find the Sum and Product of Elements in a Row/Column – Sanfoundry

This is the Java Program to Find the Sum and Product of Elements in a Row/Column.

Problem Description

Given a square matrix, print the sum and product of the elements in each row and column.
Example:
Matrix:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

Output:

Sum Product
10     24
28     585
26     1680
32     1680
42     11880
36     3465
58     43680
40     6144

Problem Solution

Traverse through the matrix, and calculate the sum and product of each row and column elements and display it.

Program/Source Code

Here is the source code of the Java Program to Find the Sum and Product of Elements in a Row/Column. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.

  1.  
  2. //Java Program to Find the Sum and Product of Elements in a Row/Column
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.InputStreamReader;
  6.  
  7. public class SumAndProduct {
  8.     // Function to calculate sum and products
  9.     static void displayOutput(int[][] matrix)
  10.     {
  11.         int i,j,sumr,productr,sumc,productc;
  12.         System.out.println("ttSum   Product");
  13.         for(i=0; i<matrix.length; i++){
  14.             sumr = sumc = 0;
  15.             productr = productc = 1;
  16.             for(j=0; j<matrix[i].length; j++){
  17.                 sumr+=matrix[i][j];
  18.                 sumc+=matrix[j][i];
  19.                 productr*=matrix[i][j];
  20.                 productc*=matrix[j][i];
  21.             }
  22.             System.out.format("%s %d   %3d   %2dn","Row",i+1,sumr,productr);
  23.             System.out.format("%s %d   %3d   %2dn","Col",i+1,sumc,productc);
  24.         }
  25.     }
  26.     // Function to read user input
  27.     public static void main(String[] args) {
  28.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  29.         int order;
  30.         System.out.println("Enter the order of the matrix");
  31.         try {
  32.             order = Integer.parseInt(br.readLine());
  33.         } catch (Exception e) {
  34.             System.out.println("An error occurred");
  35.             return;
  36.         }
  37.         int[][] matrix = new int[order][order];
  38.         System.out.println("Enter matrix elements");
  39.         int i, j;
  40.         for (i = 0; i < order; i++) {
  41.             for (j = 0; j < order; j++) {
  42.                 try {
  43.                     matrix[i][j] = Integer.parseInt(br.readLine());
  44.                 } catch (Exception e) {
  45.                     System.out.println("An error occurred");
  46.                     return;
  47.                 }
  48.             }
  49.         }
  50.         System.out.println("Tha matrix is");
  51.         for (i = 0; i < order; i++) {
  52.             for (j = 0; j < order; j++) {
  53.                 System.out.print(matrix[i][j] + "t");
  54.             }
  55.             System.out.println();
  56.         }
  57.         displayOutput(matrix);
  58.     }
  59. }

Program Explanation

1. In function displayOutput(), four variables productr,productc,sumr and sumc are created.
2. The loop for(i=0; i< matrix.length; i++) traverses through the rows of the matrix and it sets the variables productr, productc to 1 and sumr, sumc to 0.
3. The nested loop for(j=0; j<matrix[i].length; j++) calculates the sum and products of elements in each row and column.
4. Finally, the output is displayed.

Time Complexity: O(nm) where n is the number of rows and m is the number of columns in the matrix respectively.

Runtime Test Cases

 
Case 1 (Simple Test Case):
 
Enter the order of the matrix
4
Enter matrix elements
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
The matrix is
1	2	3	4	
5	6	7	8	
9	10	11	12	
13	14	15	16	
	Sum   Product
Row 1    10   24
Col 1    28   585
Row 2    26   1680
Col 2    32   1680
Row 3    42   11880
Col 3    36   3465
Row 4    58   43680
Col 4    40   6144

Sanfoundry Global Education & Learning Series – Java Programs.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Leave a Comment

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