Java Program to Find Largest Element in a Matrix

In the previous article, we have discussed Java Program to Find Smallest Element in a Matrix

In this article we are going to see how we can write a program to find out the largest element in a matrix in JAVA language.

Java Program to Find Largest Element in a Matrix

A 3*3 Matrix is having 3 rows and 3 columns where this 3*3 represents the dimension of the matrix. Means there are 3*3 i.e. total 9 elements in a 3*3 Matrix.

Let’s understand it in more simpler way.

                   | A00   A01   A02 |
Matrix A =  | A10   A11   A12 |
                   | A20   A21   A22 | 3*3
  • Matrix A represents a 3*3 matrix.
  • A‘ represents the matrix element
  • Aij‘ represents the matrix element at it’s matrix position/index.
  • i‘ represents the row index
  • j‘ represents the column index
  • Means A00=Aij  where i=0 and j=0A01=aij where i=0 and j=1 and like this.
  • Here we have started row value from 0 and column value from 0.

Let’s see different ways to find largest element in the matrix.

Method-1: Java Program to Find Largest Element in a Matrix By Static Initialization of Array Elements

Approach:

  • Initialize and declare two arrays one of size 3×3 with elements
  • Use two for loops to iterate the rows and columns .
  • Inside the for loops compare all the elements and find the largest.
  • Print the result.

Program:

public class matrix{
    public static void main(String args[])
    {
        // Initializing the 3X3 matrix i.e. 2D array
        int arr[][] = {{19,25,32},{40,54,62},{70,20,60}}, temp;
        int row, col;

        // Initializing the first element of the array in the variable to compare
        // Like we are taking arr[0][0] as largest element in matrix
        temp = arr[0][0];
        System.out.print("The matrix elements are : ");
        printMatrix(arr);

        //Finding the smallest element
        //we are comparing matrix elemnts with 'temp'
        // if any other element is greater than 'temp' then we are assigning that element to 'temp'
        for(row=0;row<3;row++)
            for(col=0;col<3;col++)
                if(temp<arr[row][col])
                    temp = arr[row][col];

        System.out.print("\nLargest element in the matrix is : "+temp);
    }

    // Method to print the matrix
    static void printMatrix(int arr[][])
    {
        int row, col;
        // Loop to print the elements
        for(row=0;row<3;row++)
        {
            // Used for formatting
            System.out.print("\n");
            for(col=0;col<3;col++)
            {
                System.out.print(arr[row][col]+" ");
            }
        }
        System.out.print("\n");
    }

}

Output:

The matrix elements are : 
19 25 32 
40 54 62 
70 20 60

Largest element in the matrix is : 70

Method-2: Java Program to Find Largest Element in a Matrix By Dynamic Initialization of Array Elements

Approach:

  • Initialize and declare two arrays one of size 3×3
  • Ask the user for input and store them in the one array using two for loops.
  • Use two for loops to iterate the rows and columns .
  • Inside the for loops compare all the elements and find the largest.
  • Print the result.

Program:

import java.util.Scanner;
public class matrix{
    public static void main(String args[])
    {
        //Scanner class to take input
        Scanner scan = new Scanner(System.in);

        // Initializing the 3X3 matrix i.e. 2D array
        int arr[][] = new int[3][3], temp;
        int row, col;
        
        // Taking matrix input
        System.out.println("\nEnter matrix elements :");
        for(row=0;row<3;row++)
            for(col=0;col<3;col++)
                arr[row][col] = scan.nextInt();

        // Initializing the first element of the array in the variable to compare
        temp = arr[0][0];
        System.out.print("The matrix elements are :");
        printMatrix(arr);

        //Finding the largest element
        for(row=0;row<3;row++)
            for(col=0;col<3;col++)
                if(temp<arr[row][col])
                    temp = arr[row][col];

        System.out.print("\nLargest element in the matrix is :"+temp);
    }

    // Method to print the matrix
    static void printMatrix(int arr[][])
    {
        int row, col;
        // Loop to print the elements
        for(row=0;row<3;row++)
        {
            // Used for formatting
            System.out.print("\n");
            for(col=0;col<3;col++)
            {
                System.out.print(arr[row][col]+" ");
            }
        }
         System.out.print("\n");
    }

}

Output:

Enter matrix elements : 91 72 36 44 29 87 67 56 17
The matrix elements are :
91 72 36 
44 29 87 
67 56 17 

Largest element in the matrix is : 91

Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language.

Related Java Programs: