loader image

Java program to find the smallest and largest element in the array

Read an array of 10 or more numbers and write a program to find the

  1. Smallest element in the array
  2. Largest element in the array
  3. Second largest element in the array
import java.util.Scanner;

public class ArrayElements {
    public static void main(String args[]) {

        // Scanner class object to read input values
        Scanner sc = new Scanner(System.in);

        // declare variables
        int i, j, temp, n = 10;
        int arr[] = new int[10];

        // read array elements from user
        System.out.print("Enter 10 elements : ");
        for (i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }

        // sort array elements
        for (i = 0; i < n; i++) {
            for (j = i + 1; j < 10; j++) {
                if (arr[i] > arr[j]) {
                    temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
        }

        // print smallest, largest and second largest element
        System.out.println("Smallest element = " + arr[0]);
        System.out.println("Largest element = " + arr[n - 1]);
        System.out.println("Second Largest element = " + arr[n - 2]);
    }
}

Output

Enter 10 elements : 9 44 5 23 65 12 7 28 92 10
Smallest element = 5
Largest element = 92
Second Largest element = 65
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Scroll to Top