In this tutorial you will learn about the Java Program to Print Name Of Month and its application with practical example.
Java Program to Print Name Of Month
In this tutorial, we will learn to create a Java program that will Print Name Of Month using Java programming.
Prerequisites
Before starting with this tutorial, we assume that you are the best aware of the following Java programming topics:
- Operators in Java Programming.
- Basic Input and Output function in Java Programming.
- Basic Java programming.
- Conditional Statements in Java programming.
- Arithmetic operations in Java Programming.
Algorithm:-
1 2 3 4 5 6 7 8 9 |
1. Declaring the required variables for the program. 2. Taking the input <strong>Month </strong>from the user. 3. Using the conditional statements to find the name of the <strong>Month</strong>. 4. Printing the <strong>Month </strong>Name in a Year. 5. End the Program. |
Find the Month Name of a year:-
In today’s program, we will take the input month in a range between 1 to 12 from the user. Then we will pass that variable to the conditional statements to find the name of the Month. At last, we will print the resulting name of the Month to the user.
With the help of the below program, we can find the name of the Month in a year.
Program Code:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
import java.util.Scanner;//Importing the packages in the program. public class MonthOfyear { public static void main(String[] args) { //Declaring the required variables for the program. int mth; Scanner scanner = new Scanner(System.in); //Taking the input month number from the user. System.out.println("Enter Month number (1-12) : "); mth = scanner.nextInt(); //Finding the Month of the year. if(mth == 1) { //Printing the Month of the year System.out.println("January"); } else if(mth == 2) { //Printing the Month of the year System.out.println("February"); } else if(mth == 3) { //Printing the Month of the year System.out.println("March"); } else if(mth == 4) { //Printing the Month of the year System.out.println("April"); } else if(mth == 5) { //Printing the Month of the year System.out.println("May"); } else if(mth == 6) { //Printing the Month of the year System.out.println("june"); } else if(mth == 7) { //Printing the Month of the year System.out.println("July"); } else if(mth == 8) { //Printing the Month of the year System.out.println("August"); } else if(mth == 9) { //Printing the Month of the year System.out.println("September"); } else if(mth == 10) { //Printing the Month of the year System.out.println("October"); } else if(mth == 11) { //Printing the Month of the year System.out.println("November"); } else if(mth == 12) { //Printing the Month of the year System.out.println("December"); } else { //Printing the message to enter month number between 1-12 System.out.println("Please enter month number between 1-12."); } } } |
Output:-
In the above program, we have first initialized the required variable.
- mth = it will hold the integer value for the input.
Taking the input month from the user.
Finding the name of the month in a year using conditional statements.
Printing the output of the program.