Java Program to Form the Largest Number from an Array of Non-Negative Integers

In the previous article, we have seen Java Program to Copy an Array in Reverse

In this article we are going to see how we can find the largest number in an array of non negative integers.

Java Program to Form the Largest Number from an Array of Non-Negative Integers

Prerequisite: 

See below articles to know more about Array, array declaration, array instantiation and array initialization.

Let’s see different ways to find largest element in the array of non negative integers.

Method-1: Java Program to Find the Largest Number in an Array By Comparing Array Elements

Approach:

  • Take an array with non negative elements in it.
  • Print the array elements.
  • Create a variable say large and store the first element of the array in it like it is the largest element for now.
  • Then start comparing the variable with the whole array, if any larger element is found than the current larger element replace this number as the largest number.
  • At last you will see large variable holding the largest number.
  • Print the largest element.

Program:

import java.util.Arrays;
import java.util.Scanner;

public class Main
{
    public static void main(String args[])
    {
        // Creating the array with non negative elements
        int arr[] = {72, 22, 94, 48, 54, 66};

        // Initializing the first element of the array to large
        int large=arr[0];        
        
        // Compares all the element to find out the largest one
        for(int i:arr)
        {
            if(large<i)
                large=i;
        }

        // Prints the array elements
        System.out.println("The array elements are"+Arrays.toString(arr));
        
        // Prints the largest element
        System.out.println("The largest element of the array is: "+large);
    }
}
Output:

The array elements are[72, 22, 94, 48, 54, 66]
The largest element of the array is: 94

Method-2: Java Program to Find the Largest Number in an Array By Using Arrays.sort Method

Approach:

  • Take an array with non negative integer elements in it.
  • Print the array elements.
  • Use Arrays.sort function to sort the array in ascending order.
  • Print the last element as that is the largest number.

Program:

 import java.util.Arrays;
import java.util.Scanner;
public class array
{
    public static void main(String args[])
    {
        // Creating the array with non negative elements
        int arr[] = {82, 42, 34, 90, 74, 56};
        
        // Sort the array in ascending order
        Arrays.sort(arr);

        // Print the array elements
        System.out.println("The array elements are"+Arrays.toString(arr));
        
        // Print the last element as that is the largest element
        System.out.println("The largest element of the array is: "+arr[arr.length-1]);
    }
}
Output:

The array elements are[34, 42, 56, 74, 82, 90]
The largest element of the array is: 90

Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.

Related Java Articles: