Home / Programs / C Program to find Second largest Number in an Array
Programming Example

C Program to find Second largest Number in an Array

👁 1,281 Views
💻 Practical Program
📘 Step by Step Learning
In this program, we will show you, How to write a C Program to find Second largest Number in an Array with an example. Before going into this example, Please refer Array in C article to understand the concept of Array size, index position etc.
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.

Program Code

/* 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;
}

Output

 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 . . .

Explanation

Nope

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.