Q:

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

belongs to collection: Java Basic Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

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

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Java program to read gender (M/F) and print the co... >>
<< Java program to convert hexadecimal byte to decima...