Count Sundays

Learn to write a code for counting Sundays during the twentieth century.

Challenge

January 1st, 1900 was a Monday. The months April, June, September, and November have 30 days, whereas the rest have 31 days, except February which has 29 or 28 depending on whether the year is a leap year or not. A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400. Write a program to find out how many Sundays fell on the first of the month during the twentieth century (January 1st, 1901 to December 31st, 2000)?

Note: It is given that January 1st, 1900 was a Monday.

For this problem, you will have to write two functions:

1. isLeap ( )

This function takes a year as an integer value, returns True if that is a leap year, and False otherwise.

2. numSundaysFirst( )

This function should return the number of months in the twentieth century that started on a Sunday. It may use isLeap () as a helper function.

Coding exercise

Try to solve this challenge on your own. If you can’t solve it, you can look at the solution and explanation below.

Get hands-on with 1400+ tech skills courses.