Advertisement

Latest Post

Tuesday, 3 January 2017

How to find the minimum and maximum element of the array[C#]

Write a program in C# to find the minimum and maximum element of the array.

Following is the simple C# code :
class Lab
    {
        static void Main(string[] args)
        {
            int[] array = new int[10];
            Console.WriteLine("enter the array elements to b sorted");
            for (int i = 0; i < 10; i++)
            {
                array[i] = Convert.ToInt32(Console.ReadLine());
            }
            int smallest = array[0];
            for (int i = 0; i < 10; i++)
            {
                if (array[i] < smallest)
                {
                    smallest = array[i];

                }
            }
            int largest = array[9];
            for (int i = 0; i < 10; i++)
            {

                if (array[i] > largest)
                {
                    largest = array[i];

                }
            }
            Console.WriteLine("the smallest no is {0}", smallest);
            Console.WriteLine("the largest no is {0}", largest);
            Console.Read();
        }
    }

Output:-
enter the elements to be sorted
1
5
75
0
15
98
45
15
26
45
the smallest no is 0
the largest no is 98


Government Jobs