In this article, you will learn how to use a switch case in the C# programming language. The given 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 Sunday, for 2 Monday, 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.
C# program to print the weekday using switch case:
using System;
namespace FindAndSolve
{
public class Program
{
/* How to compare two Dates without time in C# */
static void Main(string[] args)
{
int week;
Console.WriteLine("Enter the week day number : ");
week = Convert.ToInt32(Console.ReadLine());
switch (week)
{
case 1:
Console.WriteLine("Sundar");
break;
case 2:
Console.WriteLine("Monday");
break;
case 3:
Console.WriteLine("Tuesday");
break;
case 4:
Console.WriteLine("Wednesday");
break;
case 5:
Console.WriteLine("Thursday");
break;
case 6:
Console.WriteLine("Friday");
break;
case 7:
Console.WriteLine("Saturday");
break;
default:
Console.WriteLine("Invalid input");
break;
}
Console.ReadKey();
}
}
}
Output
Program Explanation:
- It is asking the user to enter the weekday and store that value in the variable day.
- Based on the value of the day, it moves to a case block and prints the message.
- For any invalid value, i.e. if the number is not in 1 to 7, it moves to the default block and prints that the input is invalid.