ADVERTISEMENT
ADVERTISEMENT

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 Solution:

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.

Java Basic Programs »


ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

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.