C# Program to find the largest and smallest element in an array


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


Array List is: 11 12 13 14 15

The largest element is: 15

The smallest element is: 11

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

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

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

		lar = sm = arr[0];
		for (i = 0; i < 5; i++)
		{
			if (lar < arr[i])
			{
				lar = arr[i];
			}

			if (sm > arr[i])
			{
				sm = arr[i];
			}
		}
		Console.WriteLine("Largest element is:" + lar);
		Console.WriteLine("Smallest element is:" + sm);
	}
 }

Output:

Enter five numbers:10
40
50
5
20
Largest element is:50
Smallest element is:5