• Source
    1. // Java program to find the smallest and largest element in an array
    2.  
    3. import java.util.*;
    4.  
    5. class Main
    6. {
    7. public static void main(String args[])
    8. {
    9. int large,small,i;
    10. int a[] = new int[]{1, 2, 3, 4, 5};
    11. int n = a.length;
    12. large=small=a[0];
    13. for(i=1;i<n;++i)
    14. {
    15. if(a[i]>large)
    16. large=a[i];
    17.  
    18. if(a[i]<small)
    19. small=a[i];
    20. }
    21.  
    22. System.out.print(“The smallest element is ” + small );
    23. System.out.print(“The largest element is ” + large );
    24. }
    25. }