Home » C# » C# programs » C# basic programs

C# program to input weekday number and print the weekday

C# switch statement example: Here, we are going to write a C# program – it will input a weekday number and prints weekday name.
Submitted by IncludeHelp, on April 09, 2019

Printing weekday name from weekday number

A switch statement allows checking a variable/value with a list of values (cases) and executing the block associated with that case.

Weekday number is the number value 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 input a value between 0 to 6 and check with a switch statement.

C# code to print weekday name from given weekday number (0-6)

Here, we are asking for an input of the weekday number (from 0 to 6) and print the weekday based on the given input using switch statement.

// C# program to input weekday number and print the weekday
using System;
using System.IO;
using System.Text;

namespace IncludeHelp
{
    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...





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.