basics · beginner · ~15 min

Days in a month

Group switch cases that share a result.

Challenge

Return the number of days in a month, using a switch that groups months sharing a length.

Task

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.

Input

A single int m (the month number; valid range is 1-12).

Output

Returns the day count, or 0 for an invalid month.

Example

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)

Edge cases

  • Months outside 1-12 return 0.
  • February is always 28 here (non-leap year).

Rules

  • Use switch with grouped cases (fall-through) for months that share a length.

Input format

A single int m (month number, 1-12).

Output format

The number of days in month m, or 0 if m is out of range.

Constraints

Non-leap year (February is 28). Invalid month returns 0.

Starter code

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.