Programming Examples

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


Write a Java program to accept n element on an array and find the smallest value of an array.

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

Output

output:

Enter the size of Array : 

5

Enter the 5 Elements of Array

23

34

3

23

34

Smallest Element of Array :3

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