Find the 2nd largest number in an array
int main()
{
int array[]={2,1,6,5,4,8};
int max, sec_max;
if(a[1]<a[0])
{
max=a[0];
sec_max=a[1];
}
else
{
max=a[1];
sec_max=a[0];
}
for(int i=2;i<6;i++)
{
if(a>max)
{
sec_max=max;
max=a;
}
if(a<max && a>sec_max)
{
sec_max=a;
}
}
printf("Second largest element is %d",sec_max);
}
--
Comments
Post a Comment