Find Largest and Smallest Number in an Array - Java Language Basics

Java examples for Language Basics:Array

Description

Find Largest and Smallest Number in an Array

Demo Code


public class Main {
        public static void main(String[] args) {
               /*  w w w. jav a  2s  .co m*/
                int numbers[] = new int[]{32,43,333,54,12,51,61,19,14,13};
                int smallest = numbers[0];
                int largetst = numbers[0];
               
                for(int i=1; i< numbers.length; i++)
                {
                        if(numbers[i] > largetst)
                                largetst = numbers[i];
                        else if (numbers[i] < smallest)
                                smallest = numbers[i];
                       
                }
               
                System.out.println("Largest Number is : " + largetst);
                System.out.println("Smallest Number is : " + smallest);
        }
}

Result


Related Tutorials