MVC ASP.NET C# SQL Server WCF Written Test HR Round Subscribe C# Videos C# Programs Buy DVD

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());


        }
    }
}

1 comment:

  1. Dear Teacher i'd like to ask you 2 question about C# :
    1.get 3 values from user input and find the greatest value ?
    2.Print the following :
    1*****
    12****
    123***
    1234**
    12345*
    123456
    Thank i wait to hear from you best regard !

    ReplyDelete