C# program to find the smallest and largest elements of an array using predefined methods

Here, we are going to learn how to find the smallest and largest elements of an array using predefined methods in C#?
Submitted by Nidhi, on August 27, 2020

Here we will create an array of integers and then we find the smallest and largest elements using Min() and Max() method and then print them on the console screen.

Program:

The source code to find the smallest and largest elements of the array using predefine methods is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to find the smallest and largest 
//elements of an array using predefined methods.

using System;
using System.Linq;

class Sample
{
    static void Main()
    {
        int[] MyArray = { 40,30,20,10,50 };

        int large = MyArray.Max();
        int small = MyArray.Min();

        Console.WriteLine("Largest Element  : " + large);
        Console.WriteLine("Smallest Element : " + small);
    }
}

Output:

Largest Element  : 50
Smallest Element : 10
Press any key to continue . . .

Explanation:

In the above program, we created a class Sample that contains an array of integers.

int large = MyArray.Max();
int small = MyArray.Min();

In the above code, we find the largest and smallest elements from the array. Here we need to import "System.Linq" namespace into the program.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.