Find the second largest element in an array with C.
#include <stdio.h>
#include <limits.h> // For INT_MIN
int main()
{
int arr[110], size, i;
int max1, max2;
printf("Enter size of the array (1-1000): ");
scanf("%d", &size);
/* Input array elements */
printf("Enter elements in the array: ");
for (i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
}
max1 = max2 = INT_MIN;
for (i = 0; i < size; i++)
{
if (arr[i] > max1)
{
max2 = max1;
max1 = arr[i];
}
else if (arr[i] > max2 && arr[i] < max1)
{
max2 = arr[i];
}
}
printf("First largest = %d\n", max1);
printf("Second largest = %d", max2);
return 0;
}
Output:-
Enter size of the array(1 - 1000) : 5
Enter elements in the array : 10 20 30 40 50
First largest = 50
Second largest = 40
0 Comments