C Program to print second largest element in array
C Program to print the second largest element in an array
C Program to print second largest element in array
#include <stdio.h>
#include <limits.h>
void main()
{
int arr[50], n, i, max1, max2;
printf("Enter number of array elements: ");
scanf("%d", &n);
printf("Enter array elements-\n");
for (i = 0; i < n; i++)
scanf("%d", &arr[i]);
max1 = max2 = INT_MIN;
for (i = 0; i < n; i++)
{
if (arr[i] > max1)
max1 = arr[i];
else if(arr[i] < max1 && arr[i] > max2)
max2 = arr[i];
}
printf("\nSecond largest number in array is: %d", max2);
}
Output
Enter number of array elements: 5
Enter array elements-
3
2
1
4
4
Second largest number in array is: 2