Write a program to input month number and print month Name


The program is a Java program that takes a month number input from the user and prints the corresponding month name.

  • The program starts by importing the java.util.Scanner package which allows user input to be taken from the console.
  • In the main method, the program prompts the user to enter a month number between 1 and 12 using the System.out.print method and reads the input using the Scanner object's nextInt() method.
  • The program then checks the entered month number using a series of if and else if statements. If the entered month number is between 1 and 12, the program prints the corresponding month name using the System.out.println() method.
  • If the entered month number is not between 1 and 12, the program prints "Enter 1 to 12..." using the System.out.println() method.
  • Finally, the program exits the main method, and the program execution ends.

Source Code

import java.util.Scanner;
class MonthDay
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		System.out.print("Enter the Month Number(1-12) :");
		int num = input.nextInt();			
		if(num==1)
			System.out.println("This is a January");
		else if(num==2)
			System.out.println("This is a February");
		else if(num==3)
			System.out.println("This is a March");
		else if(num==4)
			System.out.println("This is a April");
		else if(num==5)
			System.out.println("This is a May");
		else if(num==6)
			System.out.println("This is a June");
		else if(num==7)
			System.out.println("This is a July");
		else if(num==8)
			System.out.println("This is a August");
		else if(num==9)
			System.out.println("This is a September");
		else if(num==10)
			System.out.println("This is a October");
		else if(num==11)
			System.out.println("This is a November");
		else if(num==12)
			System.out.println("This is a December");
		else
			System.out.println("Enter 1 to 12...");
	}
}

Output

Enter the Month Number(1-12) :10
This is a October

Example Programs