Java Program

Object:-Write a 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.







You may also like: