Loading...
Loading...
A hostel charges rent per day, so the office needs the number of days in a month. Read a month number (1 for January to 12 for December) and a year, and print how many days that month has. February has 29 days in a leap year and 28 otherwise. A year is a leap year if it is divisible by 4 and not by 100, or divisible by 400.
If the month number is not between 1 and 12, print Invalid month.
Invalid month.Example 1
Input
4 2025
Output
30
April has 30 days.
Example 2
Input
2 2024
Output
29
2024 is a leap year, so February has 29 days.
Example 3
Input
13 2024
Output
Invalid month
There is no 13th month.
Try the problem first. Open one hint at a time.
A switch on the month with grouped cases fits well: one group for the 31-day months, one for the 30-day months, one for February.
For February, use the leap year rule inside that case.
The default case catches every invalid month number.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor