Programming Examples

Java program to accept n elements in array and find its largest and smallest Element


Write  a java program to accept n elements in array and find the largest and smallest number from array.

import java.util.*;
class MaxMin
{
    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 min=num[0];  
        int max=num[0]; 
        for(int i=1;i<n;i++)
        {
            if(min>num[i])
            {
                min=num[i];
            }
            if(max<num[i])
            {
                max=num[i];
            }
        }
        System.out.println("Largest Element of Array :"+max);
        System.out.println("Smallest Element of Array :"+min);
    }
}
Output

output:

Enter the size of Array : 

5

Enter the 5 Elements of Array

23

3

45

2

22

Largest Element of Array :45

Smallest Element of Array :2

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