C Program to find Second largest Number in an Array
This program ask the user to enter the Array size, Array elements and the Search item value. Next, it will find the Second largest Number in this Array using For Loop.
/* C Program to find Second largest Number in an Array */
#include <stdio.h>
#include <limits.h>
int main()
{
int arr[50], i, Size;
int first, second;
printf("\n Please Enter the Number of elements in an array : ");
scanf("%d", &Size);
printf("\n Please Enter %d elements of an Array \n", Size);
for (i = 0; i < Size; i++)
{
scanf("%d", &arr[i]);
}
first = second = INT_MIN;
for (i = 0; i < Size; i++)
{
if(arr[i] > first)
{
second = first;
first = arr[i];
}
else if(arr[i] > second && arr[i] < first)
{
second = arr[i];
}
}
printf("\n The Largest Number in this Array = %d", first);
printf("\n The Second Largest Number in this Array = %d\n", second);
return 0;
}
Please Enter the Number of elements in an array : 5
Please Enter 5 elements of an Array
10 90 80 90 80
The Largest Number in this Array = 90
The Second Largest Number in this Array = 80
Press any key to continue . . .
First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.
Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.