Read the question, write C on the right, then Run or Check.
QuestionHint and solution stay closed until you open them
Read a month number 1–12 and print how many days it has in a non-leap year.
February is 28. A switch (or a small array) is cleaner than a long if-chain.
Input. One integer 1–12.
Output. One integer: 28, 30, or 31.
Constraints
- 1 ≤ month ≤ 12
Examples
Input
2
Output
28
Input
4
Output
30
Input
1
Output
31
Hint
- int days[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
Show correct code
Peek only after you have tried. You can still Check your own version.
#include <stdio.h>
int main(void) {
int month;
int days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (scanf("%d", &month) == 1) {
printf("%d\n", days[month]);
}
return 0;
}
main.cC17 · gcc · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.