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


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


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

The second Smallest element is: 20

Example: How to find the second smallest 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 smallest element:" + arr[1]);
	}
}

Output:

Enter five numbers:10
30
20
40
50
Second smallest element:20