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

CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Sarkari Exam Quiz