Java program to display largest element in each row in 2-D Array

Display Largest Element in 2-D Array

In this article, we will discuss about largest element in each row in 2-D Array. Array has n-rows and m-columns , here our task is to write a program to Display the largest element in each row in a 2-D Array in Java.

  • Sample input: a=[[1,2,3],[6,5,4],[8,9,7]]
  • Sample output: 3 6 9
Largest element in 2-D Array in java


  • Take the row and column of a matrix as input.
  • Take  row*column space separated elements as input.
  • Declare max variable and initialize it with the Minimum value of Integer.
  • Traverse each element of a row and print it.
  • At the end all the max element of each row get printed.

Code in Java

import java.util.*;
class Solution
public static void main(String[] args)
Scanner sc=new Scanner(;
System.out.println("Enter the number of rows");
int row=sc.nextInt();
System.out.println("Enter the number of columns");
int col=sc.nextInt();
int matrix[][]=new int[row][col];
System.out.println("Enter the elements of matrix");
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
int max=Integer.MIN_VALUE;

System.out.println("Max Value of each row");
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
System.out.print(max+" ");

Output :

Enter the number of rows
Enter the number of columns
Enter the elements of matrix
1 2 3
6 5 4
8 9 7
Max Value of each row
3 6 9