Find 1st ,2nd and Third Maximum Number in Array using C++
========================================================================
#include<iostream>
using namespace std;
main()
{
int arr[5]={99,88,12,11,6};
int max=arr[0],max2=arr[0],max3=arr[0];
for(int i=0;i<5;i++)
{
if(arr[i]>max)
{
max=arr[i];
}
}
cout<<"1st Maximum Nmumber = "<<max;
if(max2==max)
{
max2=arr[1];
}
for(int i=0;i<5;i++)
{
if(arr[i]<max)
{
if(arr[i]>max2)
{
max2=arr[i];
}
}
}
cout<<"\n2nd Maximum Number = "<<max2<<endl;
if(max3==max2 || max3==max)
{
max3=arr[2];
}
for(int i=0;i<5;i++)
{
if(arr[i]<max2)
{
if(arr[i]>max3)
{
max3=arr[i];
}
}
}
cout<<"3rd Maximum Number = "<<max3;
}
=======================================================================
0 comments:
Post a Comment