Dremendo Tag Line

Input day number and print day name in Java

if else if - Question 3

In this question, we will see how to input a number between 1 to 7 and print the corresponding day of a week (day name) in Java programming using the if else if statement. To know more about if else if statement click on the if else if statement lesson.

Q3) Write a program in Java to input a number between 1 to 7 and print the corresponding day of a week (day name).

Example:
1 = Sunday
2 = Monday
3 = Wednesday

Program

import java.util.Scanner;

public class Q3
{
    public static void main(String args[])
    {
        int n;
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter a number ");
        n=sc.nextInt();
        if(n==1)
        {
            System.out.print("Sunday");
        }
        else if(n==2)
        {
            System.out.print("Monday");
        }
        else if(n==3)
        {
            System.out.print("Tuesday");
        }
        else if(n==4)
        {
            System.out.print("Wednesday");
        }
        else if(n==5)
        {
            System.out.print("Thursday");
        }
        else if(n==6)
        {
            System.out.print("Friday");
        }
        else if(n==7)
        {
            System.out.print("Saturday");
        }
        else
        {
            System.out.print("Invalid number");
        }
    }
}

Output

Enter a number 5
Thursday
video-poster