This is a Java Program to Find the Largest Number in an Array.
Enter the elements of array as input. By comparing elements of array with each other we get the largest number of the array.
Here is the source code of the Java Program to Find the Largest Number in an Array. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
import java.util.Scanner;
public class Largest_Number
{
public static void main(String[] args)
{
int n, max;
Scanner s = new Scanner(System.in);
System.out.print(“Enter number of elements in the array:”);
n = s.nextInt();
int a[] = new int[n];
System.out.println(“Enter elements of array:”);
for(int i = 0; i < n; i++)
{
a[i] = s.nextInt();
}
max = a[0];
for(int i = 0; i < n; i++)
{
if(max < a[i])
{
max = a[i];
}
}
System.out.println("Maximum value:"+max);
}
}
Output:
$ javac Largest_Number.java
$ java Largest_Number
Enter number of elements in the array:5
Enter elements of array:
4
2
3
6
1
Maximum value:6