Java Program for finding the largest element of the array

Finding the Largest element of the array

Here we’ll code a Java program for finding the largest element of the array. We’ll consider an integer array and compare all the elements of the array for finding the largest element. Its a pretty simple code, let’s see an algorithm for finding the largest element of the array.

Java program for finding the largest number in the array

Algorithm for Finding the largest element of the array

  • Step 1 – Initialize a variable len, for storing the length of the array.
  • Step 2 – Accept the length of the array.
  • Step 3 – Initialize an array.
  • Step 4 – Accept the input, using for loop.
  • Step 5 – Store the first element of the array in min.
  • Step 6 – Initialize a variable large, for storing the largest element of the array.
  • Step 7 – Compare all the elements of the array with large, and use a if condition for comparing all the array elements with large.
  • Step 7 – If there’ll be any element larger than large, store it value in large.
  • Step 8 – Print large.

JAVA Program for finding the Largest number in an array

import java.util.*;
public class first 
{
    public static void main(String[] args) 
    {
        System.out.print("Enter the length of the array : ");
        Scanner sc=new Scanner(System.in);
        int len=sc.nextInt();
        int[] arr=new int[50];
        System.out.print("Enter the elements of the array : ");
        for(int i=0;i<len;i++)
        {
            arr[i]=sc.nextInt();
        }
        int large = arr[0];
        for(int i=0;i<len;i++) { if(arr[i]>large)
            {
                large=arr[i];
            } 
        }
        System.out.print("The largest element of the array : "+large);
    }
}
Output

Enter the length of the array : 5
Enter the elements of the array : 
2
35
45
877
54
The largest element of the array : 877