C# Program to find the second largest element in an array


In this program, You will learn how to find the second largest element in an array in C#.


Array List is: 10 20 30 40 50 60 70 80 90 100

The second Largest element is: 90

Example: How to find the second largest element in an array in C#.

using System;
public class Program
{
	public static void Main(string[] args)
	{
		int i, j, temp;
		int[] arr = new int[5];

		Console.Write("Enter five numbers:");
		for (i = 0; i < arr.Length; i++)
		{
			arr[i] = Convert.ToInt32(Console.ReadLine());
		}

		for (i = 0; i < 5; i++)
		{
			for (j = i + 1; j < 5; j++)
			{
				if (arr[i] > arr[j])
				{
					temp = arr[i];
					arr[i] = arr[j];
					arr[j] = temp;
				}
			}
		}
		Console.WriteLine("Second largest element is:" + arr[5 - 2]);
	}
}

Output:

Enter five numbers:10
30
40
50
20
Second largest element is:40