We are providing online training of realtime Live project on Asp.Net MVC with Angular and Web API. For more information click here. If you have any query then drop the messase in CONTACT FORM

Monday, March 27, 2017

How to find the second largest number of the given 0 to 9 numbers in c#

using System;

namespace Example
{
    class SecondLargestNum
    {
        static void Main()
        {
            int largeNum = 0;
            int smallNum = 0;

            Console.WriteLine("Enter the numbers");
            int num = int.Parse(Console.ReadLine());


            while (num > 0)
            {
                var mod = num % 10;

                if (mod > largeNum)
                {
                    smallNum = largeNum;
                    largeNum = mod;
                }

                if (mod != largeNum && smallNum < mod)
                {
                    smallNum = mod;
                }
                num = num / 10;

            }
            Console.WriteLine("Second largest number {0}: ", smallNum);
            Console.ReadLine();
        }
    }
} 



Output:

No comments: