Wednesday 22 April 2015

How to find the Greatest and the smallest number in a given Array in c# asp.net ?

Here I am writing a program in c# to find out the Greatest and the samllest number in gievn array.This program will ask to enter the size of Array then will all numbers one by finally it will print the results.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApp
{
    class Program
    {
        public static void Main()
        {
            int n;
            float largest, smallest;
            int[] a = new int[50];
            Console.WriteLine("Please enter the size of Array");
            string s = Console.ReadLine();
            n = Int32.Parse(s);
            for (int i = 0; i < n; i++)
            {
                Console.WriteLine("Please enter the {0} elements of the Array", + i+1);
                string s1 = Console.ReadLine();
                a[i] = Int32.Parse(s1);
            }
            Console.Write("");
            largest = a[0];
            smallest = a[0];
            for (int i = 1; i < n; i++)
            {
                if (a[i] > largest)
                    largest = a[i];
                else if (a[i] < smallest)
                    smallest = a[i];
            }
            Console.WriteLine("The Largest element in the array is {0}", largest);
            Console.WriteLine("The Smallest element in the array is {0}", smallest);
            Console.ReadKey();
        }
    }

}


Output




No comments:

Post a Comment