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