Programming Examples

Java program that prints the month name based on the input month serial number


Write a Java program that prints the month name based on the input month serial number (1 for January, 2 for February, ..., 12 for December) using a switch statement:

Solution

import java.util.Scanner;
public class MonthNameSwitch 
{
    public static void main(String[] args) 
	{
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the month number (1-12): ");
        int monthNumber = sc.nextInt();
        String monthName;
        switch (monthNumber) 
		{
            case 1:
                monthName = "January";
                break;
            case 2:
                monthName = "February";
                break;
            case 3:
                monthName = "March";
                break;
            case 4:
                monthName = "April";
                break;
            case 5:
                monthName = "May";
                break;
            case 6:
                monthName = "June";
                break;
            case 7:
                monthName = "July";
                break;
            case 8:
                monthName = "August";
                break;
            case 9:
                monthName = "September";
                break;
            case 10:
                monthName = "October";
                break;
            case 11:
                monthName = "November";
                break;
            case 12:
                monthName = "December";
                break;
            default:
                monthName = "Invalid month number";
                break;
        }
        System.out.println("Month name for month number " + monthNumber + " is: " + monthName);
    }
}
Output

Enter the month number (1-12): 3

Month name for month number 3 is: March

Online Exam Quiz for One day Exam Online Typing Test CCC Online Test 2026 Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Best Website and Software Company in Allahabad