Java Program to Find Smallest and Largest Element in an Array

import java.util.Scanner; //import Scanner class in our program
class demo
{
 public static void main(String…s)
 {
 int i,n,large,small;
 Scanner sc=new Scanner(System.in); //used to read from keyboard
 System.out.print(“Enter number of elements:”);
 n=sc.nextInt();
 int a[]=new int[n];

 System.out.print(“Enter elements of Array:”);
 for(i=0; i large)
   large = a[i];
  if(a[i] < small)
   small = a[i];
 }
 System.out.print(“Smallest Element:”+small);
 System.out.println(“Largest Element:”+large);
 }
}

Output

Enter number of elements: 5
Enter elements of Array:
10 20 2 80 8

Explanation

In this Program we have find out the largest and smallest element from the array.