Dremendo Tag Line

Input number and print the corresponding month name using switch case in Java

switch case - Question 1

In this question, we will see how to input an integer between 1 to 12 and print the corresponding name of month in Java programming using the switch case statement. To know more about switch case statement click on the switch case statement lesson.

Q1) Write a program in Java to input an integer between 1 to 12 and print the corresponding name of month using the switch case statement.

Program

import java.util.Scanner;

public class Q1
{
    public static void main(String args[])
    {
        int n;
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter a number between 1 to 12 : ");
        n=sc.nextInt();
        switch(n)
        {
            case 1:
                System.out.print("January");
                break;

            case 2:
                System.out.print("February");
                break;

            case 3:
                System.out.print("March");
                break;

            case 4:
                System.out.print("April");
                break;

            case 5:
                System.out.print("May");
                break;

            case 6:
                System.out.print("June");
                break;

            case 7:
                System.out.print("July");
                break;

            case 8:
                System.out.print("August");
                break;

            case 9:
                System.out.print("September");
                break;

            case 10:
                System.out.print("October");
                break;

            case 11:
                System.out.print("November");
                break;

            case 12:
                System.out.print("December");
                break;

            default:
                System.out.print("Invalid Input");
        }
    }
}

Output

Enter a number between 1 to 12 : 8
August
video-poster