Find The Greatest And Smallest Number In Integer Array and find Difference between largest and smallest.
 class Program
    {
        static void Main(string[] args)
        {
            int n,largest, smallest;
            int[] a = new int[50];
            Console.WriteLine("Enter the size of Array");
            n = Int32.Parse(Console.ReadLine());
            Console.WriteLine("Enter the array elements");
            for (int i = 0; i < n; i++)
            {
                a[i] = Int32.Parse(Console.ReadLine());
                Console.WriteLine();
            }
            Console.WriteLine("");
            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("Largest element in the array is {0}", largest);
            Console.WriteLine("Smallest element in the array is {0}", smallest);        
            Console.WriteLine("Difference: {0}",largest - smallest);
            Console.ReadLine();
        }
    }

 
 
No comments:
Post a Comment