The Program in C# Sharp to read any Month Number in integer and display Month name in the word is given below:
using System;
namespace MonthName
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello Codeauri Family, enter a month number (1-12) to display Months name below:");
int monthNumber = int.Parse(Console.ReadLine());
switch (monthNumber)
{
case 1:
Console.WriteLine("January");
break;
case 2:
Console.WriteLine("February");
break;
case 3:
Console.WriteLine("March");
break;
case 4:
Console.WriteLine("April");
break;
case 5:
Console.WriteLine("May");
break;
case 6:
Console.WriteLine("June");
break;
case 7:
Console.WriteLine("July");
break;
case 8:
Console.WriteLine("August");
break;
case 9:
Console.WriteLine("September");
break;
case 10:
Console.WriteLine("October");
break;
case 11:
Console.WriteLine("November");
break;
case 12:
Console.WriteLine("December");
break;
default:
Console.WriteLine("Invalid month number. Please enter a number between 1 and 12.");
break;
}
}
}
}
Output:
Hello Codeauri Family, enter a month number (1-12) to display Months name below:
8
August
Pro-Tips💡
Here are the step by step execution of above program:
- The first line “using System;” is a using directive that references the System namespace.
- The next line defines a namespace named “MonthName”.
- Within the “MonthName” namespace, there is a class named “Program”.
- Inside the class, there is a Main method, which is the entry point of the program.
- Within the Main method, it first writes a message to the console asking the user to enter a month number.
- The next line uses the “int.Parse” method to convert the user input to an integer and stores it in the “monthNumber” variable.
- The switch statement checks the value of the “monthNumber” variable and prints the corresponding month name for each case, based on the number entered.
- If the value of the “monthNumber” does not match any of the cases, the default case is executed, printing an error message to the console.
- The “break” statement terminates each case and moves the program control to the end of the switch statement.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.