C# program to print the weekday using switch case

C# program to get the day number as input and print the weekday using switch case:

In this post, we will learn how to use a switch case in C#. The program will take one number as input from the user and it will print the day name using a switch block.

For example, if the user enters 1, it will print Monday, for 2 Tuesday etc. For an invalid input, i.e. if it is not in between 1 to 7, it will print a message that the input is invalid.

Let’s write down the program:

C# program to print the weekday using switch case:

Below is the complete C# program:

using System;

public class Program
{
	public static void Main()
	{
		int day;
        
        Console.WriteLine("Enter the week day number : ");
		day = Convert.ToInt32(Console.ReadLine());
		
		switch(day){
			case 1:
				Console.WriteLine("Monday");
				break;
			case 2:
				Console.WriteLine("Tuesday");
				break;
			case 3:
				Console.WriteLine("Wednesday");
				break;
			case 4:
				Console.WriteLine("Thursday");
				break;
			case 5:
				Console.WriteLine("Friday");
				break;
			case 6:
				Console.WriteLine("Saturday");
				break;
			case 7:
				Console.WriteLine("Sunday");
				break;
			default:
				Console.WriteLine("Invalid input");
				break;
		}
	}
}

Explanation:

Here,

  • It is asking the user to enter the week day and storing that value in the variable day.
  • Based on the value of day, it moves to a case block and prints the message.
  • For any invalid value, i.e. if the number is not in 1,2,3,4,5,6,7, it moves to the default block and prints that the input is invalid.

Sample output:

It will print output as like below:

Enter the week day number : 
4
Thursday

Enter the week day number : 
99
Invalid input

You might also like: