C# Console Switch Statement

Get Month Name From Month Number – C#

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:

Explanation of the Code

  1. User Input:
    • The program asks the user to input a number between 1 and 12

 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:

    • Each case corresponds to a specific month number. If the input does not match any case, the default block executes.
  1. Break Statement:
    • The break statement terminates the current case block, ensuring the program doesn’t fall through to the next case.
  2. 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:

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.