In Java, to find number of days in a given input month we can use if-else-if condition, Ternary Operator or Switch statements. In this article, we will learn through examples, logic and program explanation for better understanding.
For example, if the input number is 1, it corresponds to January, which has 31 days.
Required Knowledge
- Java programming basics
- Java Operators
- Conditional Statements (if-else and Switch)
- Ternary Operator
Problem Statement
We need to write a java program to find number of days in a given month. The Program should take input values from 1 to 12 and output the number of days accordingly. This action will be performed in our program. Let’s look at an example,
Example 1:
Input : 1
There are 31 days in this month
Here, for the input 1, Output is “There are 31 days in this month” as the number 1 is considered as january.
Example 2:
Input: 4
There are 30 days in this month
Here, for the input 4, Output is “There are 30 days in this month” as the number 4 is considered as April.
Logic to find number of days in a given month in java
There are multiple logics to find number of days in a given month in Java Programing language.
Logic 1: Using if-else-if
- After taking the month number as input from the user, we need to compare it with the numbers 1 through 12 (which represent the months of the year).
- If the number is equal to 1, it is considered January and the program prints ’31 days in this month’. If the user enters 2, the program will print ’28 or 29 days (leap year) for February. Similarly, for the input 12, the program will output ’31 days in this month’ for December.
- If the user enters a number outside the range of 1 to 12, the program will ask the user to enter a number within this range.
int month = sc.nextInt();
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
System.out.println("There are 31 days in this month");
else if (month == 4 || month == 6 || month == 9 || month == 11)
System.out.println("There are 30 days in this month");
else if(month==2)
System.out.println("There are either 28 or 29(leap year) days in this month");
else
System.out.println("Entered invalid number, please select in the range of 1 to 12");
Logic 2: Using Ternary Operator
- The ternary operator takes three operands and works similar to the if else statements. If condition true, it prints first expression and if condition becomes false then it prints second expression.
- We first take the month number as input from the user. Using the ternary operator, we compare this input with values 1 through 12.
- If the input matches any of these values (e.g., 1 for January), the ternary operator assigns the corresponding message ‘the month has 31 days’ to the result variable.
- This process repeats for all 12 months, ensuring that the correct number of days is assigned to the result based on the input month number.
int month = sc.nextInt();
String result;
result = (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)?
month+" There are 31 days in this month":
(month == 4 || month == 6 || month == 9 || month == 11)?
month +" There are 30 days in this month":
(month==2)?
month+ " There are either 28 or 29(leap year) days in this month":
"Entered invalid number, please select in the range of 1 to 12";
System.out.println(result);
Logic 3: Using Switch Statements
- Switch is a value based conditional statement, each case represents one value and , here we are checking equal to condition indirectly.
- Each case represents a specific month number, and when a match is found, it executes the corresponding code (like printing the number of days in that month).
- If none of the case values match the input, the default case handles this by printing an error message. Each case ends with a break statement to prevent further execution.
switch(month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
System.out.println(month +": There are 31 days in this month");
break;
case 4:
case 6:
case 9:
case 11:
System.out.println(month +": There are 30 days in this month");
break;
case 2:
System.out.println(month +": either 28 or 29 days in this month");
break;
default:
System.out.println("Entered invalid month number, please correct it");
}
Java Program to find number of days in a given month
Program 1: Using if-else-if
In the below program we are using if-else-if to find number of days in a given month.
import java.util.Scanner;
public class NoOfDaysMonth{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Month number from 1 to 12");
int month = sc.nextInt();
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
System.out.println("There are 31 days in this month");
else if (month == 4 || month == 6 || month == 9 || month == 11)
System.out.println("There are 30 days in this month");
else if(month==2)
System.out.println("There are either 28 or 29(leap year) days in this month");
else
System.out.println("Entered invalid number, please select in the range of 1 to 12");
sc.close();
}
}
Output:
Enter the Month number from 1 to 12
13
Entered invalid number, please select in the range of 1 to 12
Java program explanation to find number of days in a given month
- The program imports the
Scanner
class from thejava.util
package to facilitate user input. - Then it defines a class named
NoOfDaysMonth
. - The program contains the main method where the program execution starts.
- Asks the user to enter a month number from 1 to 12.
- It uses a series of
if-else-if
statements to determine the number of days in the given month. - If the month is January, March, May, July, August, October, or December (represented by month numbers 1, 3, 5, 7, 8, 10, or 12), it prints that there are 31 days in the month.
- Else, If the month is April, June, September, or November (represented by month numbers 4, 6, 9, or 11), it prints that there are 30 days in the month.
- Otherwise, if the month is February (represented by month number 2), it prints that there are either 28 or 29 days depending on whether it’s a leap year.
- If the input number is outside the range of 1 to 12, it prints a message prompting the user to select a number in the valid range.
- It closes the
Scanner
objectsc
to release system resources.
Program 2: Using Ternary Operator
In the below program we are using Ternary Operator to find number of days in a given month.
import java.util.Scanner;
public class NoOfDaysMonth {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Month number from 1 to 12");
int month = sc.nextInt();
String result;
result = (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)?
month+" There are 31 days in this month":
(month == 4 || month == 6 || month == 9 || month == 11)?
month +" There are 30 days in this month":
(month==2)?
month+ " There are either 28 or 29(leap year) days in this month":
"Entered invalid number, please select in the range of 1 to 12";
System.out.println(result);
sc.close();
}
}
Output:
Enter the Month number from 1 to 12
7
There are 31 days in this month
Java program explanation to find number of days in a given month
- The program imports the
Scanner
class from thejava.util
package to facilitate user input. - Then it defines a class named
NoOfDaysMonth
. - Program contains the main method where the program execution starts.
- Asks the user to enter a month number from 1 to 12.
- It uses ternary operators to determine the number of days in the given month.
- Program checks if the month is January, March, May, July, August, October, or December (represented by month numbers 1, 3, 5, 7, 8, 10, or 12), and assigns the corresponding message indicating there are 31 days.
- It checks if the month is April, June, September, or November (represented by month numbers 4, 6, 9, or 11), and assigns the corresponding message indicating there are 30 days.
- Else, if the month is February (represented by month number 2), and assigns the corresponding message indicating there are either 28 or 29 days depending on whether it’s a leap year.
- If the input number is outside the range of 1 to 12, it assigns a message prompting the user to select a number in the valid range.
- Program prints the value of the
result
variable. - It closes the
Scanner
objectsc
to release system resources.
Program 3: Using Switch Statement
In the below program we are using switch statement to find number of days in a given month.
import java.util.Scanner;
public class InterviewExpert {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Month number from 1 to 12");
int month = sc.nextInt();
switch(month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
System.out.println(month +": There are 31 days in this month");
break;
case 4:
case 6:
case 9:
case 11:
System.out.println(month +": There are 30 days in this month");
break;
case 2:
System.out.println(month +": either 28 or 29 days in this month");
break;
default:
System.out.println("Entered invalid month number, please correct it");
}
sc.close();
}
}
Output:
Enter the Month number from 1 to 12
4
4: There are 30 days in this month
Java program explanation to print number of days for a given input month
- This Java program prompts the user to input a month number from 1 to 12.
- It utilizes a switch-case statement to handle different cases based on the month input.
- For months with 31 days (January, March, May, July, August, October, December), it prints a message indicating there are 31 days in that month.
- And for months with 30 days (April, June, September, November), it prints a message indicating there are 30 days in that month.
- For February (month number 2), it prints a message indicating there are either 28 or 29 days, depending on whether it’s a leap year or not.
- If the input doesn’t correspond to any valid month number (outside the range of 1 to 12), it prints an error message asking the user to correct the input.
- The Scanner is closed at the end of the program to release the resources it holds.
Conclusion
In this article, we explored how to find number of days in a given month using if-else-if statements, ternary operator and the switch() statement. We understood the logic behind this and moved on to writing the program.
For more information on related topics, you might find these articles helpful:
- Understanding the concept of Conditional statements
- Concepts of Operators in Java
- Top 10 Java Programming Tips
We hope you find this article helpful. If you have any questions or need any further clarification, feel free to leave a comment below. Don’t forget to subscribe our newsletter to get more updates on Java Programming tips and tutorials.
Happy Coding!