Number of Days in a Month - Problem

Given a year and a month, return the number of days in that month.

You need to handle leap years correctly. A leap year occurs:

  • Every 4 years, BUT
  • NOT every 100 years, BUT
  • Every 400 years

For example, 2000 and 2004 are leap years, but 1900 is not.

Remember that February has 28 days in regular years and 29 days in leap years.

Input & Output

Example 1 — Leap Year February
$ Input: year = 2020, month = 2
Output: 29
💡 Note: 2020 is a leap year (divisible by 4, not by 100), so February has 29 days
Example 2 — Regular Month
$ Input: year = 2019, month = 8
Output: 31
💡 Note: August always has 31 days regardless of leap year
Example 3 — Non-Leap February
$ Input: year = 1900, month = 2
Output: 28
💡 Note: 1900 is not a leap year (divisible by 100 but not by 400), so February has 28 days

Constraints

  • 1583 ≤ year ≤ 2100
  • 1 ≤ month ≤ 12

Visualization

Tap to expand
Days in Month CalculatorINPUTYear: 2020Month: 2LOGICFeb + Leap Year2020 % 4 = 0 ✓OUTPUT29Leap Year Rules• Divisible by 4: Usually leap year• Divisible by 100: NOT leap year• Divisible by 400: IS leap year2020: 2020 % 4 = 0, 2020 % 100 ≠ 0 → Leap Year
Understanding the Visualization
1
Input
Year and month values
2
Logic
Check leap year for February, use fixed values for others
3
Output
Number of days in that month
Key Takeaway
🎯 Key Insight: February is the only month affected by leap years - use modulo arithmetic to check leap year conditions efficiently
Asked in
Google 15 Microsoft 12 Amazon 8
26.8K Views
Medium Frequency
~8 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen