Java Program to Display the weekday between 1 and 7

In this post, we are going to take input from a user between 1 to 2 and print the day name base on weeks no. If the user enters 1 that means it is Monday.

We are going to see the two programs. First program using Nested if-else in which value from the user program check the week number using if-else condition and display the corresponding days.

In the second program we are using switch case in which value from the user program check the week number and display the corresponding days.

Example,

Input1

  1. Enter weekdays number: 1
  2. Monday

Input2

  1. Enter weekdays number :4
  2. Thursday

Example 1: Program to display weekday between 1 to 7 using if-else

The below program ask the user to enter the valid week number. After getting the value from the user program check the week number using the switch case and display the corresponding day. Like the above program also I assumed that Monday is the first day of the week.

import java.util.Scanner;
public class Weekday{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter weekday number : ");
        int weekday = scanner.nextInt();

        if(weekday == 1) {

            System.out.println("Monday");

        } else if(weekday == 2) {

            System.out.println("Tuesday");

        } else if(weekday == 3) {

            System.out.println("Wednesday");

        } else if(weekday == 4) {

            System.out.println("Thursday");

        } else if(weekday == 5) {

            System.out.println("Friday");

        } else if(weekday == 6) {

            System.out.println("Saturday");

        } else if(weekday == 7) {

            System.out.println("Sunday");

        } else {

            System.out.println("Please enter weekday number between 1-7.");
        }
    }
}

Example 2: Program to display weekday between 1 to 7 using switch-case

The below program used a const string array to store the days on the corresponding array index. Now ask the user to enter the valid week number. After getting the value from the user get the days from the array using this value as an array index and display the day. Like the above program also I assumed that Monday is the first day of the week.

import java.util.Scanner;
public class Weekday_between_1_to_7 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter weekday number : ");
        int weekday = scanner.nextInt();

        switch (weekday)
        {
            case 1:
                System.out.println("Monday");
                break;
            case 2:
                System.out.println("Tuesday");
                break;
            case 3:
                System.out.println("Wednesday");
                break;
            case 4:
                System.out.println("Thursday");
                break;
            case 5:
                System.out.println("Friday");
                break;
            case 6:
                System.out.println("Saturday");
                break;
            case 7:
                System.out.println("Sunday");
                break;
            default:
                System.out.println("Please enter weekday number between 1-7.");
        }
    }
}

Recommended:

  1. Java Program to solve quadratic equations
  2. Java Program to find the greatest of three Numbers
  3. Check if the given number is prime or not in Java
  4. Java Program Count invalid Mobile numbers form list
  5. Java program Fill a bucket with water using a mug
  6. Java Program to Check if Number is Positive or Negative

Leave a Comment

Your email address will not be published. Required fields are marked *

x