Printing weekday name from weekday number
A switch proclamation permits checking a variable/esteem with a rundown of qualities (cases) and executing the square connected with that case.
Weekday number is the number an incentive from 0 to 6, 0 for “Sunday”, 1 for “Monday”, 2 for “Tuesday”, 3 for “Wednesday”, 4 for “Thursday”, 5 for “Friday” and 6 for “Saturday”. We will include an incentive between 0 to 6 and check with a switch proclamation.
C# code to print weekday name from given weekday number (0-6)
Here, we are requesting a contribution of the weekday number (from 0 to 6) and print the weekday dependent on the given info utilizing switch proclamation.
Program
// C# program to input weekday number and print the weekday
using System;
using System.IO;
using System.Text;
namespace JustTechReview
{
class Test
{
// Main Method
static void Main(string[] args)
{
int wday;
//input wday number
Console.Write("Enter weekday number (0-6): ");
wday = Convert.ToInt32(Console.ReadLine());
//validating using switch case
switch (wday)
{
case 0:
Console.WriteLine("It's SUNDAY");
break;
case 1:
Console.WriteLine("It's MONDAY");
break;
case 2:
Console.WriteLine("It's TUESDAY");
break;
case 3:
Console.WriteLine("It's WEDNESDAY");
break;
case 4:
Console.WriteLine("It's THURSDAY");
break;
case 5:
Console.WriteLine("It's FRIDAY");
break;
case 6:
Console.WriteLine("It's SATURDAY");
break;
//if no case value is matched
default:
Console.WriteLine("It's wrong input...");
break;
}
//hit ENTER to exit the program
Console.ReadLine();
}
}
}
Output
First run:
Enter weekday number (0-6): 0
It's SUNDAY
Second run:
Enter weekday number (0-6): 4
It's THURSDAY
Third run:
Enter weekday number (0-6): 6
It's SATURDAY
Fourth run:
Enter weekday number (0-6): 9
It's wrong input...