Tuesday , February 28 2017
Home / java programs / Java Program To Find Largest and Smallest Number in Array

Java Program To Find Largest and Smallest Number in Array

Here we are going to write a program to find Largest and Smallest Number in Array.

Find Largest and Smallest Number :


public class FindSmallestAndLargestElementInArray {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] data = {10,20,5,15,22,30,30,0,1,};
        findSmallestAndLargestElementInArray(data);
    }
    
    public static void findSmallestAndLargestElementInArray(int[] data) {
        if (data != null && data.length > 0) {
            int smallest = data[0];
            int largest = data[0];
            for (Integer element : data) {
                if (element < smallest) {
                    smallest = element;
                }

                if (element > largest) {
                    largest = element;
                }
            }

            System.out.println("Smallest: " + smallest + " largest: " + largest);
        }
    }
}

Output :

Smallest: 0 largest: 30

About chandrashekhar

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Check Also

Java Program to Find the GCD of Two Numbers

Here we are going to find GCD of two numbers in Java. Find GCD of two …

Leave a Reply

Your email address will not be published. Required fields are marked *