Q:

write c# program to read number of month from user, and prints the equavalent month name. solve it in 2 ways, first way using if else, second way using switch case

0

write c# program to read number of month from user, and prints the equavalent month name.

solve it in 2 ways, first way using if else, second way using switch case

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

internal class Program
{
    private static void Main(string[] args)
    {
        //1st way: using if else:
        int monthnumber;
        Console.Write("enter month number (between 1 to 12): ");
        monthnumber = Convert.ToInt32(Console.ReadLine());
        if (monthnumber == 1)
            Console.WriteLine("JAN");
        else if (monthnumber == 2)
            Console.WriteLine("FEB");
        else if (monthnumber == 3)
            Console.WriteLine("MAR");
        else if (monthnumber == 4)
            Console.WriteLine("APRL");
        else if (monthnumber == 5)
            Console.WriteLine("MAY");
        else if (monthnumber == 6)
            Console.WriteLine("JUNE");
        else if (monthnumber == 7)
            Console.WriteLine("JULY");
        else if (monthnumber == 8)
            Console.WriteLine("AGUST");
        else if (monthnumber == 9)
            Console.WriteLine("SEP");
        else if (monthnumber == 10)
            Console.WriteLine("OCT");
        else if (monthnumber == 11)
            Console.WriteLine("NOV");
        else if (monthnumber == 12)
            Console.WriteLine("DECMP");
        else
            Console.WriteLine("invalid date");
    }
}

 

using switch statement:

internal class Program
{
    private static void Main(string[] args)
    {
        int monthnumber;
        Console.Write("enter month number (between 1 to 12): ");
        monthnumber = Convert.ToInt32(Console.ReadLine());
        switch (monthnumber)
        {
            case 1: 
                Console.WriteLine("JAN");
                break;
            case 2: 
                Console.WriteLine("FEB");
                break;
            case 3: 
                Console.WriteLine("MAR");
                break;
            case 4: 
                Console.WriteLine("APR");
                break;
            case 5:
                Console.WriteLine("MAY");
                break;
            case 6:
                Console.WriteLine("JUNE");
                break;
            case 7: 
                Console.WriteLine("JULY");
                break;
            case 8:
                Console.WriteLine("AGUST");
                break;
            case 9:
                Console.WriteLine("SEP");
                break;
            case 10:
                Console.WriteLine("OCT");
                break;
            case 11:
                Console.WriteLine("NOV");
                break;
            case 12:
                Console.WriteLine("DECM");
                break;
            default: 
                Console.WriteLine("invalid entry");
                break;
        }
    }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now