CSharp (C#) If..else..if ladder


An If..Else..If ladder is implemented in most of the programming logics. The execution happens from top to bottom. Once a condition is satisfied, remaining if else statements are all bypassed and the control moves to the next statement outside the if..else..if ladder. If none of the conditions are satisfied, then the final else will be executed.

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

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter the number between 2 and 10 : ");
            int number = Convert.ToInt32(Console.ReadLine());
            if (number % 2 == 0)
            {
                Console.WriteLine("The smallest factor of {0} is 2",number);
            }
            else if (number % 3 == 0)
            {
                Console.WriteLine("The smallest factor of {0} is 3",number);
            }
            else if (number % 5 == 0)
            {
                Console.WriteLine("The smallest factor of {0} is 5",number);
            }
            else if (number % 7 == 0)
            {
                Console.WriteLine("The smallest factor of {0} is 7",number);
            }
            else
            {
                Console.WriteLine("The number isnt between 1 and 10");
            }

            Console.ReadLine();
        }
    }

}

Output:

C Sharp ( C# ) Control Statements

No comments: