This is the Java Program to Find the Sum and Product of Elements in a Row/Column.
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
Traverse through the matrix, and calculate the sum and product of each row and column elements and display it.
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.
//Java Program to Find the Sum and Product of Elements in a Row/Column
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class SumAndProduct {
// Function to calculate sum and products
static void displayOutput(int[][] matrix)
{
int i,j,sumr,productr,sumc,productc;
System.out.println("\t\tSum Product");
for(i=0; i<matrix.length; i++){
sumr = sumc = 0;
productr = productc = 1;
for(j=0; j<matrix[i].length; j++){
sumr+=matrix[i][j];
sumc+=matrix[j][i];
productr*=matrix[i][j];
productc*=matrix[j][i];
}
System.out.format("%s %d %3d %2d\n","Row",i+1,sumr,productr);
System.out.format("%s %d %3d %2d\n","Col",i+1,sumc,productc);
}
}
// Function to read user input
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int order;
System.out.println("Enter the order of the matrix");
try {
order = Integer.parseInt(br.readLine());
} catch (Exception e) {
System.out.println("An error occurred");
return;
}
int[][] matrix = new int[order][order];
System.out.println("Enter matrix elements");
int i, j;
for (i = 0; i < order; i++) {
for (j = 0; j < order; j++) {
try {
matrix[i][j] = Integer.parseInt(br.readLine());
} catch (Exception e) {
System.out.println("An error occurred");
return;
}
}
}
System.out.println("Tha matrix is");
for (i = 0; i < order; i++) {
for (j = 0; j < order; j++) {
System.out.print(matrix[i][j] + "\t");
}
System.out.println();
}
displayOutput(matrix);
}
}
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.
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.
- Practice BCA MCQs
- Practice Programming MCQs
- Practice Information Technology MCQs
- Apply for Computer Science Internship
- Check Java Books