Monday, September 10, 2012

Write C# method to display second largest and second smallest number in a given array.

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

namespace ConsoleApplication11
{
    class Program
    {
        static void Main(string[] args)
        {

            int temp;
            int[] sort = new int[5];
            Console.WriteLine("enter the number");
            for (int i = 0; i < sort.Length; i++)
                sort[i] = int.Parse(Console.ReadLine());
            for (int i = 0; i < sort.Length; i++)
            {
                for (int j = i + 1; j < sort.Length; j++)
                {
                    if (sort[i] > sort[j])
                    {
                        temp = sort[i];
                        sort[i] = sort[j];
                        sort[j] = temp;
                    }
                }
            }
            Console.WriteLine("the output");
            for (int i = 0; i < sort.Length; i++)
                Console.WriteLine(sort[i]);

            Console.WriteLine("the second smallest number is {0}", sort[1]);
            Console.WriteLine("the second largest number is {0}", sort[3]);
            Console.ReadKey();
        }
    }
}

No comments:

Post a Comment