Days in Month
Programming Basics · C++
TASK
Problem
Given month `m` and year `y`, print the number of days in that month, using Gregorian leap-year rules for February.
EXAMPLE
Example
2 2024
29
LIMITS
Constraints
1 ≤ m ≤ 12; 1 ≤ y ≤ 10^9.
LEARN
Theory for this problem
+
These exercises combine arithmetic, comparisons, and small branches. Keep each condition explicit and easy to verify.
Connection to “Days in Month”: here, branching selects a case using a Boolean condition; test order matters when cases overlap.
NEED HELP?
Hints
+
['Break the task into a few simple checks instead of compressing everything into a clever expression.', 'After solving “Days in Month”, verify the algorithm on your own small example and on an allowed boundary case. Print only the required result with no extra text.']
ANSWER
Solution
+
#include <iostream>
using namespace std;
int main() {
int m,y;
cin >> m >> y;
int d;
if(m==2) d=(y%400==0||(y%4==0&&y%100!=0))?29:28;
else if(m==4||m==6||m==9||m==11) d=30;
else d=31;
cout << d << '\n';
return 0;
}