Java Program to find largest and smallest number in array

Java Program to find largest and smallest number in array

import java.util.*;  
public class Main {  
    public static void main(String[] args) {  
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter the size of array: ");
      int n = sc.nextInt();
      int arr[] = new int[n];
      System.out.println("Enter " +n+ " array elements ");
      for(int i=0; i<n; i++) {
         arr[i] = sc.nextInt();
      }
      int largest = arr[0];
      int smallest = arr[0];
      for(int i=1;i<n;i++){
      if(arr[i]>largest){
            largest =arr[i];
      }
      if(arr[i]<smallest){
            smallest = arr[i];
      }
    }
    System.out.println(largest+" is largest and "+ smallest +" is smallest");
  }
}    

Output

Enter the size of array: 
5
Enter 5 array elements 
2
4
1
44
2
44 is largest and 1 is smallest