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 [Last updated : March 18, 2023]

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...

C# Basic Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.