Find smallest and largest number in an integer array
I want a c# program that can find and print, the smallest and largest number in a given integer array.
using System;
using System.Linq;
namespace SamplePrograms
{
class LargestSmallest
{
public static void Main()
{
// Declare and initialize the integer array
int[] NumbersArray = { 102, 34, 89, 12, 187, 29, 111};
// Sort the array, first element in the array will be
// smallest and the last element will be largest
Array.Sort(NumbersArray);
// Print the smallest number in the array
Console.WriteLine("Samllest Number = {0}", NumbersArray[0]);
// Print the largest number in the array.
Console.WriteLine("Largest Number = {0}", NumbersArray[NumbersArray.Length -1]);
// Linq makes this much easier, as we have Min() and Max() extension methods
// Console.WriteLine("Samllest Number = {0}", NumbersArray.Min());
// Console.WriteLine("Largest Number = {0}", NumbersArray.Max());
}
}
}
using System.Linq;
namespace SamplePrograms
{
class LargestSmallest
{
public static void Main()
{
// Declare and initialize the integer array
int[] NumbersArray = { 102, 34, 89, 12, 187, 29, 111};
// Sort the array, first element in the array will be
// smallest and the last element will be largest
Array.Sort(NumbersArray);
// Print the smallest number in the array
Console.WriteLine("Samllest Number = {0}", NumbersArray[0]);
// Print the largest number in the array.
Console.WriteLine("Largest Number = {0}", NumbersArray[NumbersArray.Length -1]);
// Linq makes this much easier, as we have Min() and Max() extension methods
// Console.WriteLine("Samllest Number = {0}", NumbersArray.Min());
// Console.WriteLine("Largest Number = {0}", NumbersArray.Max());
}
}
}
0 comments:
Post a Comment