- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find product of Each Row and Column in Java
In Java, Array is an object. It is a non-primitive data type which stores values of similar data type. The matrix in java is nothing but a multi-dimensional array which represents multiple rows and columns.
Here we have given a matrix which contains set of elements and as per the problem statement we have to find the product of each row elements and columns elements.
In this article we will see, how it can be done by using Java programming language.
To show you some instances
Instance-1
Given matrix =
21 22 23 24 25 26 27 28 29
Product of each row −
The product of the row-1 is: 10626
The product of the row-2 is: 15600
The product of the row-3 is: 21924
Product of each column −
The product of the column-1 is: 13608
The product of the column-2 is: 15400
The product of the column-3 is: 17342
Instance-2
Given matrix =
9 2 2 4 1 7 2 6 2 2 4 3 1 4 7 8
Product of each row −
The product of the row 1 is: 144
The product of the row 2 is: 84
The product of the row 3 is: 48
The product of the row 4 is: 224
Product of each column −
The product of the column 1 is: 18
The product of the column 2 is: 112
The product of the column 3 is: 112
The product of the column 4 is: 576
Instance-3
Given matrix =
1 2 3 4 5 6 7 8 9
Product of each row −
The product of the row is: 6
The product of the row is: 120
The product of the row is: 504
Product of each column −
The product of the column 1 is: 28
The product of the column 2 is: 80
The product of the column 3 is: 162
Algorithm
Step-1 − Declares and initializes a 2D matrix called inputMatrix.
Step-2 − Loop through each row of the matrix and calculates the product of the elements in each row. The product of each row is then printed to the console.
Step-3 − Repeats the same logic for columns and calculates the product of the elements in each column.
Step-4 − The product of each column is then printed to the console with a different message.
Syntax
The Matrix.length method in Java returns the length of the given matrix.
Below refers to the syntax of it −
inputMatrix.length
where, ‘inputMatrix’ refers to the given matrix.
Multiple Approaches
We have provided the solution in different approaches.
By Using Static Initialization of Matrix Elements
By Using User Defined Method
Let’s see the program along with its output one by one.
Approach-1: By Using Static Initialization of Matrix Elements
In this approach, matrix elements will be initialized in the program. Then as per the algorithm calculate the product of elements of the rows and columns of that matrix.
Example
public class Main { public static void main(String[] args) { //declare and initialized a 2d matrix int[][] inputMatrix = {{1, 2, 3}, {1, 2, 3}, {1, 2, 3}}; System.out.println("Product of each row:"); //initiate the loop to find the rows product values for (int[] r : inputMatrix) { int prod = 1; for (int value : r) { prod *= value; } System.out.println("The product of the row is: " + prod); } System.out.println("\nProduct of each column:"); //initiate the loop to find the columns product values for (int a = 0; a < inputMatrix[0].length; a++) { int prod = 1; for (int b = 0; b < inputMatrix.length; b++) { prod *= inputMatrix[b][a]; } System.out.println("The product of the column " + (a + 1) + " is: " + prod); } } }
Output
Product of each row: The product of the row is: 6 The product of the row is: 6 The product of the row is: 6 Product of each column: The product of the column 1 is: 1 The product of the column 2 is: 8 The product of the column 3 is: 27
Approach-2: By Using User Defined Method
In this approach, matrix elements will be initialized in the program. Then call a user defined method by passing the matrix as parameter and inside method as per the algorithm calculate the product of elements of the rows and columns of that matrix.
Example
public class Main { public static void main(String[] args) { int[][] inputMatrix = {{11, 22, 33}, {44, 55, 66}, {77, 88, 99}}; //call the user-defined methods to print the product values findRowProduct(inputMatrix); findColumnProduct(inputMatrix); } //user-defined method to find the product of row elements private static void findRowProduct(int[][] mat) { System.out.println("Product of each row:"); for (int[] r : mat) { int prod = 1; for (int value : r) { prod *= value; } System.out.println("The product of the row is: " + prod); } } //user-defined method to find the product of column elements private static void findColumnProduct(int[][] mat) { System.out.println("\nProduct of each column:"); for (int a = 0; a < mat[0].length; a++) { int prod = 1; for (int b = 0; b < mat.length; b++) { prod *= mat[b][a]; } System.out.println("The product of the column " + (a + 1) + " is: " + prod); } } }
Output
Product of each row: The product of the row is: 7986 The product of the row is: 159720 The product of the row is: 670824 Product of each column: The product of the column 1 is: 37268 The product of the column 2 is: 106480 The product of the column 3 is: 215622
In this article, we explored different approaches to find the product of each row and column elements of a matrix by one by using Java programming language.