In this article we will learn how to convert month number to a month name in C# Console Application with switch case statement.
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | class Program { static void Main(string[] args) { int monthNumber; Console.Write("Enter Month Number (1 - 12): "); monthNumber = Convert.ToInt32(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("you did not enter correct value for month name"); break; } Console.ReadLine(); } } |
Output:
Alternative Solution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | class Program { static void Main(string[] args) { int monthNumber; Console.Write("Enter Month Number (1 - 12): "); monthNumber = Convert.ToInt32(Console.ReadLine()); string[] monthName = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; if(monthNumber>0 && monthNumber<13) { Console.WriteLine(monthName[monthNumber-1]); } Console.ReadLine(); } } |
Online Editor: