[Solved] Maximum Element In Each Row with Java

Write a program to find the maximum element in each row of the matrix.

Input Format:

The input consists of (m*n+2) integers. The first integer corresponds to m, the number of rows in the matrix and the second integer corresponds to n, the number of columns in the matrix. The remaining integers correspond to the elements in the matrix. The elements are read in rowwise order, first row first, then second row and so on. Assume that the maximum value of m and n is 10.

Output Format:

Refer sample output for details.

Sample Input 1:

3

2

4 5

6 9

0 3

Sample Output 1:

5

9

3

Solution

import java.util.Scanner;
import java.io.*;

public class Main {
    public static void main(String argv[]) {
        int m, n, i, j, max;
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        m = sc.nextInt();
        int[][] matrix = new int[10][10];
        for (i = 0; i < m; i++) {
            for (j = 0; j < n; j++) {
                matrix[i][j] = sc.nextInt();
            }
        }
        for (i = 0; i < m; i++) {
            max = matrix[i][0];
            for (j = 1; j < n; j++) {
                if (max < matrix[i][j]) {
                    max = matrix[i][j];
                }
            }
            System.out.println(max);
        }
    }
}

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

πŸ“Œ Nodejs and Android 😎
πŸ“Œ Java

Articles: 444

Leave a Reply

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