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

Xiith is created for educational, experimental, and schooling purpose. Examples on Xiith are made easier to make a better or basic understanding. Tutorials, testimonials, and examples are continuously checked to avoid delusion, but we cannot take complete responsibility of all programs on Xiith. When you are operating this site, you have to agree to read and accept our terms of use, cookie and privacy policy.Copyright © 2020 by Xiith. All Rights Reserved. About us.