Programming Examples
Java program to accept n elements in array and find its largest Element
Write a Java program which accept the n numbers from user and store in array and find the largest number.
Solutions:
import java.util.*;
class Max
{
public static void main(String arr[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of Array : ");
int n=sc.nextInt();
int num[]=new int[n];
System.out.println("Enter the "+n+" Elements of Array");
for(int i=0;i<n;i++)
{
num[i]=sc.nextInt();
}
int max=num[0];
for(int i=1;i<n;i++)
{
if(max<num[i])
{
max=num[i];
}
}
System.out.println("Largest Element of Array :"+max);
}
}
Output
Enter the 5 Elements of Array
34
2
33
54
5
Largest Element of Array :54