Question
Print largest elements of each row of integer matrix.
Enter size of integer matrix:
4
Enter Elements in matrix:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Elements in matrix:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
The Largest value of row 1 is=4
The Largest value of row 2 is=8
The Largest value of row 3 is=12
The Largest value of row 4 is=16
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
import java.util.Scanner;
public class LargestNumberInEachRow
{
public static void main()
{
int[ ][ ] arr;
int size,max=0;
int i,j;
Scanner sc=new Scanner(System.in);
System.out.println("Enter size of integer matrix:");
size=sc.nextInt();
arr=new int[size][size];
System.out.println("Enter Elements in matrix:");
for(i=0;i< size;i++)
{
for(j=0;j< size;j++)
{
arr[i][j]=sc.nextInt();
}
}
System.out.println("Elements in matrix:");
for(i=0;i< size;i++)
{
for(j=0;j< size;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
for(i=0;i< size;i++)
{
max=arr[i][0];
for(j=0;j< size;j++)
{
if(arr[i][j]>max)
{
max=arr[i][j];
}
}
System.out.println("The Largest value of row "+(i+1)+" is="+max);
}
}
}