C++ bootcamp · Lab 46

Days in a month

easySwitch10 minLesson: Switch

Read the question, write C++ on the right, then Run or Check.

QuestionHint and solution stay closed until you open them

Read month 1–12 and print days in a non-leap year (Feb = 28).

Input. One integer 1–12.

Output. 28, 30, or 31.

Examples

Example 1
Input
2
Output
28
Example 2
Input
4
Output
30
Example 3
Input
1
Output
31
Hint
  1. Use an array of 13 day counts, or switch.
Show correct code

Peek only after you have tried. You can still Check your own version.

#include <iostream>

int main() {
  int m; std::cin >> m;
  int days[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
  std::cout << days[m] << "\n";
  return 0;
}
main.cppC++17 · g++ · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.