Java Program to Find Largest Array Number

Write a Java Program to Find Largest Array Number with an example. Or how to write a Java Program to print or return the largest element or item in a given array.

In this Java largest array number example, we allow the user to enter the lrg_arr size and the array items. Next, we assigned the lrg_arr first array item to the Largest variable. Then, we compare each array item with this variable. If the Largest is less than any other lrg_arr array item, we replaced the Largest variable value with that lrg_arr array item.

package ArrayPrograms;

import java.util.Scanner;

public class LargestElement {
	private static Scanner sc;
	
	public static void main(String[] args) {
		int Size, i, Largest, Position = 0;
		
		sc = new Scanner(System.in);		
		System.out.print("\nPlease Enter the LRG Array size : ");
		Size = sc.nextInt();
		
		int[] lrg_arr = new int[Size];
		
		System.out.format("\nEnter LRG Array %d elements  : ", Size);
		for(i = 0; i < Size; i++) {
			lrg_arr[i] = sc.nextInt();
		}
		Largest = lrg_arr[0];
		for(i = 1; i < Size; i++) {
			if(Largest < lrg_arr[i]) {
				Largest = lrg_arr[i];
				Position = i;
			}
		}
		System.out.format("\nLargest element in LRG Array = %d", Largest);
		System.out.format("\nIndex position of the Largest element = %d", Position);
	}
}
Java Program to find Largest Array Number 1

Java Program to Find Largest Array Number example 2

In this Java largest array item example, we created a separate function to return the largest item in the given array. Please refer to the C Program to Find Largest Number in an Array article to understand the For loop iteration.

package ArrayPrograms;

import java.util.Scanner;

public class LargestElementUsingMethods {
	private static Scanner sc;
	static int Size, i, Largest, Position = 0;	
	public static void main(String[] args) {
		int[] lrg_arr = new int[10];
		
		sc = new Scanner(System.in);		
		System.out.print("\nPlease Enter the LRG Array size :  ");
		Size = sc.nextInt();
		
		System.out.format("\nEnter LRG Array %d elements : ", Size);
		for(i = 0; i < Size; i++) {
			lrg_arr[i] = sc.nextInt();
		}
		Largest = LargestElement(lrg_arr);
		System.out.format("\nLargest element in LRG Array = %d", Largest);
	}
	public static int LargestElement(int[] lrg_arr ) {
		Largest = lrg_arr[0];
		for(i = 1; i < Size; i++) {
			if(Largest < lrg_arr[i]) {
				Largest = lrg_arr[i];
				Position = i;
			}
		}
		return Largest; 
	}
}

Java largest array item output


Please Enter the LRG Array size :  7

Enter LRG Array 7 elements : 10 19 67 44 89 77 11

Largest element in LRG Array = 89