Simvik

Days in a month

Pick one of many cases with a switch statement. An easy embedded C problem (Decisions and loops), run and graded in the browser.

easy · Decisions and loops · switch

Return the number of days in month month, where 1 is January and 12 is December. Ignore leap years, so February has 28 days. Return 0 for any number that is not a month.

A switch statement is a tidy way to choose between many fixed values.

Starter code

int days_in_month(int month) {
  switch (month) {
    case 2:
      return 28;
    default:
      return 0;
  }
}

Example tests

January

printf("%d\n", days_in_month(1));

Expected output:

31

February

printf("%d\n", days_in_month(2));

Expected output:

28

Learn it first

  • Decisions and loops · Choosing with if and switch, repeating with while and for, and the loop that never ends.

More Decisions and loops problems