Java

Write a program to find a second highest number from given array.


import java.util.*;

class SecondHighestNumber {
    public static void main(String[] args) {
        int array[] = new int[10];
        int first_highest;
        int second_highest;

        Scanner sc = new Scanner(System.in);

        for(int i = 0; i < array.length; i++) {
            System.out.print("Enter element " + (i+1) + " : ");
            array[i] = sc.nextInt();
        }

        first_highest = array[1];

        for(int i = 0; i < array.length; i++) {
            if(first_highest < array[i]) {
                first_highest = array[i];
            }    
        }
        second_highest = first_highest - 1;
        for(int j = 0; j < array.length; j++) {
            if(second_highest < array[j] && array[j] < first_highest) {
                 second_highest = array[j];
             }
        }

        System.out.println();
        //System.out.println("Highest Number is : " + first_highest);
        System.out.println("Second Highest Number is : " + second_highest);
    }
}

Output :


Post a Comment

0 Comments