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