Back to Challenge Set 17

Code Challenge: Month Calendar

Difficulty  

divider

The Brief

What they asked for
Our club newsletter has a little calendar of the month in
the corner. Every month I lay it out by hand and get it
wrong. Tell it how many days the month has and which day
the 1st lands on, and it prints the grid.

Write your requirements list before you write any code. Input, calculate, output, and anything you had to decide for yourself.


Notes

  • Two parameters: the number of days, and the day of the week the 1st lands on, as a number. Decide whether Sunday is 0 or 1 and write it down.
  • Every day takes the same width. A one-digit day needs a space in front, and you have no padding tools, so an if does it.
  • The hard part is knowing when a week ends. Activity 1.6's % answers it: work out which column each day lands in.
  • Test a month that starts on a Sunday and one that ends on a Saturday. That is where an extra blank line, or a missing line break, shows up.

Sample Output

Sample Output
Su Mo Tu We Th Fr Sa
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30

This is September 2027: 30 days, starting on a Wednesday, with Sunday counted as 0. February 2026 starts on a Sunday and ends on a Saturday — try it.


Submit

Your requirements list and your program file, named cc-month-calendar.

Commence Challenge