In this program, You will learn how to find the second smallest element in an array in C#.
C# Code: How to find the second smallest element in an array in C#.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | 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:
1 2 3 4 5 6 7 8 | Enter five numbers:10 30 20 40 50 Second smallest element:20 |