Java Program to find largest number in an array


In this program, You will learn how to find largest number in an array in Java.


List is: 1 2 3 4 5

Largest number is : 5


Example: How to find largest number in an array in Java

Run it Now
import java.util.Scanner;

class Main {
    public static void main(String args[]) {

        Scanner s = new Scanner(System.in);
        int a[] = new int[100];
        int n, largest = 0;

        System.out.print("Enter Size of Array :");
        n = s.nextInt();

        System.out.print("\nEenter Array Elements :");

        for (int i = 0; i < n; i++) {
            a[i] = s.nextInt();
        }
        
        for (int i = 0; i < n; i++) {
            if (largest <= a[i]) {
                largest = a[i];
            }
        }

        System.out.print("\nLargest Number :" + largest);
    }
}

Output:

Enter Size of Array :5
Eenter Array Elements :44 55 74 8 34
Largest Number :74