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

Find Largest and Smallest Number :

[java]

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);
        }
    }
}
[/java]

Output :

Smallest: 0 largest: 30

Happy Learning 🙂