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


        }
    }
}

3 comments:

  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
    Replies
    1. store it in a array and use sort method this will sort it in ascending using index get the highest number

      Delete
  2. var n = 6;
    for (var i = 1; i <= n; i++)
    {
    for (var j = 1; j <= n; j++)
    {
    if (j <= i)
    {
    Console.Write(j);
    }
    else
    {
    Console.Write('*');
    }
    }

    Console.WriteLine();
    }

    ReplyDelete