The Program in C# Sharp to read any day number in integer and display day name in the word is given below:
using System;
namespace DayName
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello Codeauri Family, Enter a number here for a day of the week: ");
int dayNumber = int.Parse(Console.ReadLine());
switch (dayNumber)
{
case 1:
Console.WriteLine("Wow, its Monday");
break;
case 2:
Console.WriteLine("Wow, its Tuesday");
break;
case 3:
Console.WriteLine("Wow, its Wednesday");
break;
case 4:
Console.WriteLine("Wow, its Thursday");
break;
case 5:
Console.WriteLine("Wow, its Friday");
break;
case 6:
Console.WriteLine("Wow, its Saturday");
break;
case 7:
Console.WriteLine("Wow, its Sunday");
break;
default:
Console.WriteLine("Invalid day number");
break;
}
Console.ReadLine();
}
}
}
Output:
Hello Codeauri Family, Enter a number here for a day of the week:
7
Wow, its Sunday
Pro-Tips💡
Here are the step by step execution of above program:
- The code starts by including the System namespace using the “using System;” statement.
- It then defines a namespace called “DayName”.
- Within the namespace, it defines a class named “Program”.
- The Main method is defined within the Program class. This is the entry point for the program.
- The program writes a prompt to the console for the user to enter a day number.
- It then reads the user’s input, converts it to an integer using
int.Parse
, and stores it in thedayNumber
variable. - The program uses a switch statement to check the value of the
dayNumber
variable. - Depending on the value of
dayNumber
, the program outputs the name of the corresponding day of the week. - If the value of
dayNumber
is not in the range of 1 to 7, the program outputs an “Invalid day number” message. - The program waits for the user to press enter before ending by using Console.ReadLine().
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.