Java Program to find the largest number in an array


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


List is: 1 2 3 4 5

The largest number is: 5

Example: How to find the largest number in an array in java.

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 an array:");
        n = s.nextInt();

        System.out.print("Enter 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.println("Largest number:" + largest);
    }
}

Output:

Enter size of an array:5
Enter array elements:10 20 50 30 40
Largest number:50

Xiith is created for educational, experimental, and schooling purpose. Examples on Xiith are made easier to make a better or basic understanding. Tutorials, testimonials, and examples are continuously checked to avoid delusion, but we cannot take complete responsibility of all programs on Xiith. When you are operating this site, you have to agree to read and accept our terms of use, cookie and privacy policy.Copyright © 2020 by Xiith. All Rights Reserved. About us.