×

Java Programs

Java Practice

Java program to read a weekday number and print weekday name using switch statement

Given/input weekday number, we have to print weekday name using switch statement.
Submitted by Nidhi, on March 02, 2022

Problem statement

In this program, we will read an integer number for a weekday and print the corresponding weekday using a switch statement.

Source Code

The source code to read a weekday number and print a weekday name using a switch statement is given below. The given program is compiled and executed successfully.

// Java program to read a weekday number and 
// print weekday name using switch statement

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner SN = new Scanner(System.in);
    int wDay = 0;

    System.out.printf("Enter weekday number (0-6): ");
    wDay = SN.nextInt();

    switch (wDay) {
    case 0:
      System.out.printf("Sunday");
      break;

    case 1:
      System.out.printf("Monday");
      break;

    case 2:
      System.out.printf("Tuesday");
      break;

    case 3:
      System.out.printf("Wednesday");
      break;

    case 4:
      System.out.printf("Thursday");
      break;

    case 5:
      System.out.printf("Friday");
      break;
    case 6:
      System.out.printf("Saturday");
      break;

    default:
      System.out.printf("Invalid weekday number.");
    }
    System.out.printf("\n");
  }
}

Output

Enter weekday number (0-6): 5
Friday

Explanation

In the above program, we imported the "java.util.Scanner" package to read input from the user. And, created a public class Main. It contains a static method main().

The main() method is an entry point for the program. Here, we read an integer number for a weekday using Scanner class. Then we printed the corresponding weekday.

Java Basic Programs »



Related Programs



Comments and Discussions!

Load comments ↻





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