This article explains a C# program that takes a month number as input and displays the corresponding month name. It uses the switch
statement, making it a simple yet effective example for understanding control flow in programming.
Code Example
Here is the complete C# program:
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 58 59 | using System; 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 a correct value for the month."); break; } Console.ReadLine(); } } |
Explanation of the Code
- User Input:
- The program asks the user to input a number between 1 and 12
1 2 3 4 | Console.Write("Enter Month Number (1 - 12): "); monthNumber = Convert.ToInt32(Console.ReadLine()); |
Convert.ToInt32()
converts the user input from a string to an integer.
Switch Statement:
The switch
statement matches the user input (monthNumber
) with predefined cases:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | switch (monthNumber) { case 1: Console.WriteLine("January"); break; case 2: Console.WriteLine("February"); break; // Additional cases for other months default: Console.WriteLine("You did not enter a correct value for the month."); break; } |
- Each case corresponds to a specific month number. If the input does not match any case, the
default
block executes.
- Each case corresponds to a specific month number. If the input does not match any case, the
- Break Statement:
- The
break
statement terminates the current case block, ensuring the program doesn’t fall through to the next case.
- The
- Output:
- Based on the input, the program prints the corresponding month name or an error message for invalid input.
Sample Input and Output
Enter Month Number (1 – 12): 11
November
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(); } } |