Solved programming problem

Find 1st,2nd and 3rd Max value of an Array in Java.

Source code

package max1.pkg2.pkg3;

import java.util.Scanner;

public class Max123 {

   

    public static void main(String[] args) {

       

        Scanner s =new Scanner(System.in);

        int Array[] = new int[100];

       

        System.out.println("Enter Array Size: ");

        int size = s.nextInt();  

       

        System.out.println("Enter Array Element: ");

        for(int i=0;i<size;i++)

        {

            Array[i] = s.nextInt();

         

        }

     

        int max = Array[0];

        int max1= Array[0];

        int max2 = Array[0];

       

        for(int i=0;i<size;i++)

        {

             

            if (max < Array[i])

            {

                max2=max1;

                max1=max;

                max=Array[i];          

            }                        

        }      

     

        System.out.println("Max: "+max+"
2nd Max: "+max1+"
3rd Max: "+max2);

       

    }

   

}

Output

Enter array size:

5

Enter array elements:

12 32 42 23 132

Max: 132

2nd Max: 42

3rdMax: 32

Join the discussion

Add your comment