Finding and print 2nd largest element in the array - C-Tutorial

Latest

Sunday, 8 December 2019

Finding and print 2nd largest element in the array


/*  Program for 2nd largest element in the array */

#include<stdio.h>
#include<conio.h>

void main()
{
int i,n,a[30],max1,max2;
clrscr();
printf("\n Enter no.of elements: ");
scanf("%d",&n);
printf("\n Enter %d array elements : ",n);
for(i=0;i<=n-1;i++)
scanf("%d",&a[i]);
max1 = max2 = a[0];
for(i=0;i<=n-1;i++)
{
if(a[i]>max1)
{
max2=max1;
max1=a[i];
}
else
if(a[i] > max2 && a[i] < max1)
{
max2=a[i];
}
}
printf("\n Second largest number = %d",max2);
getch();
}

No comments:

Post a Comment