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

🖥 Program 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
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

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.

🔥 Practice suggestion

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.