C++ Program to find the second highest element in a given array [DEVCPP/GCC]

INT_MIN

As the name suggests, it refers to the minimum value of integer datatype.

PROGRAM

//Program to find the second highest element in a given array

#include <iostream>

using namespace std;

int main()
{
        int num [10],i,max,SecondMax;
        max=SecondMax=INT_MIN;

// Calculating the highest element of the array
        for(i=0 ; i<10 ; i++)
        {
                cout<<"ENTER "<< i<<" ELEMENT: ";
                cin>>num [i];

                if(num [i]>max)
                {
                        max=num [i];
                }
        }
 
        // Calculating the second highest element
        for(i=0;i<10;i++)
        {
                if(num [i]==max)
                {
                        continue;
                }
                else if(num [i]>SecondMax)
                {
                        SecondMax=num [i];
                 }
        }

        cout<<"\nMAXIMUM ELEMENT IS "<<max;
        cout<<"\nSECOND MAXIMUM ELEMENT IS "<<SecondMax;

        return 0;
}

OUTPUT

C++ Program to find the second highest element in a given array with output


Share this

Related Posts

FIND US ON FACEBOOK!