In this post of Java program to find maximum element in each row of the matrix.We will see how to write a java program to find the maximum element in each row of the matrix.
You may also like –
How to convert number to words in java
How to swap two numbers with or without temporary variable in java
How to reverse a number in Java
How to check Armstrong number java program
Java program to find factorial of a number
For example if the input matrix is as follows-
1 2 3 |
10 5 80 10 69 20 60 20 25 |
Then the output should be –
1 2 3 |
Maximum element in row 1- 80 Maximum element in row 2- 69 Maximum element in row 3- 60 |
Java program to find maximum element in each row of the matrix
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
package com.kkjavatutorials.client; import java.util.Scanner; /** * @author KK JavaTutorials * Java program to find maximum element in each row of the matrix */ public class MaxElementInEachRowOfMatrixTest { public static void main(String[] args) { int numberOfRows; int numberOfColumns; Scanner scanner = null; int[][] inputMatrixArray = null; try { scanner = new Scanner(System.in); System.out.println("Enter number of rows in matrix:"); numberOfRows = scanner.nextInt(); System.out.println("Enter number of columns in matrix:"); numberOfColumns = scanner.nextInt(); inputMatrixArray = new int[numberOfRows][numberOfColumns]; System.out.println("Enter matrix Elements:"); for (int i = 0; i < numberOfRows; i++) { System.out.println("Enter numbers for row-" + (i + 1)); for (int j = 0; j < numberOfColumns; j++) { inputMatrixArray[i][j] = scanner.nextInt(); } } } catch (Exception e) { e.printStackTrace(); }finally { if(scanner != null) scanner.close(); } //Displaying the entered matrix System.out.println("Matrix you have entered::"); for (int i = 0; i < inputMatrixArray.length; i++) { System.out.println(); for (int j = 0; j < inputMatrixArray[i].length; j++) { System.out.print(inputMatrixArray[i][j] + " "); } } System.out.println(); findMaxElementFromEachRowInMatrix(inputMatrixArray); } //Method to find maximum element in each row of the matrix private static void findMaxElementFromEachRowInMatrix(int[][] inputMatrixArray) { int[] arrayToStoreMaxElementInEachRow = new int[inputMatrixArray.length]; for (int i = 0; i < inputMatrixArray.length; i++) { //Initially assign first element of the row as maximum in first iteration int maxNumber = inputMatrixArray[i][0]; for (int j = 0; j < inputMatrixArray[i].length; j++) { if(maxNumber < inputMatrixArray[i][j]){ maxNumber = inputMatrixArray[i][j]; } arrayToStoreMaxElementInEachRow[i] = maxNumber; } } //Printing maximum element in each row of the matrix for (int i = 0; i < arrayToStoreMaxElementInEachRow.length; i++) { System.out.println("Maximum element in row number-" + (i + 1) + " is - " + arrayToStoreMaxElementInEachRow[i]); } } } |
Above program output for one sample input matrix:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
Enter number of rows in matrix: 3 Enter number of columns in matrix: 3 Enter matrix Elements: Enter numbers for row-1 12 78 30 Enter numbers for row-2 80 50 45 Enter numbers for row-3 100 120 30 Matrix you have entered:: 12 78 30 80 50 45 100 120 30 Maximum element in row number-1 is - 78 Maximum element in row number-2 is - 80 Maximum element in row number-3 is - 120 |
That’s all about this topic Java program to find maximum element in each row of the matrix. If you have any doubts or any suggestions to make please drop a comment.