basics · beginner · ~15 min
Group switch cases that share a result.
Return the number of days in a month, using a switch that groups months sharing a length.
Implement int days_in_month(int m) that returns the number of days in month m for a non-leap year: 31 for Jan/Mar/May/Jul/Aug/Oct/Dec, 30 for Apr/Jun/Sep/Nov, and 28 for February. Return 0 for any m outside 1-12. No main — the grader calls it.
A single int m (the month number; valid range is 1-12).
Returns the day count, or 0 for an invalid month.
days_in_month(1) -> 31 (January)
days_in_month(2) -> 28 (February, non-leap)
days_in_month(4) -> 30 (April)
days_in_month(13) -> 0 (invalid)
switch with grouped cases (fall-through) for months that share a length.A single int m (month number, 1-12).
The number of days in month m, or 0 if m is out of range.
Non-leap year (February is 28). Invalid month returns 0.
int days_in_month(int m) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.