Problem Set 9: Leap Year Checker

When you start a new problem set, your first instinct might be to open your computer and begin typing code right away. While this can feel productive, it often leads to frustration when things don't work as expected. Instead, take a few minutes to slow down and plan.

Here are some helpful strategies:

  1. Understand the problem clearly
    • Read the instructions carefully — twice if needed.
    • Ask yourself: What exactly is being asked?
  2. Break the problem into smaller steps
    • Think about the smallest possible actions the computer will need to perform.
    • For example: If the task is to find the first recurring letter in a word, what steps must happen first?
  3. Try solving it on paper first
    • Write out your steps in plain language (pseudocode).
    • Test your steps with a simple example before touching the keyboard.
  4. Translate your steps into code
    • Start small — write only a few lines at a time and test often.
    • Don't worry about perfection at first; get a working version, then improve it.
  5. Check your solution
    • Run it with different examples, including edge cases.
    • Ask: Does this solve the problem in all situations?

  • Read the question carefully (twice).
  • Break the task into the smallest steps.
  • Sketch or write pseudocode before coding.
  • Start small — test as you go.
  • Check your solution with different cases.

Problem Description

Some years have 366 days instead of 365 – these are called leap years.
Leap years are special because they add an extra day in February (February 29).

The rules for leap years are:

  1. A year is a leap year if it is divisible by 4.
  2. But if the year is also divisible by 100, then it is NOT a leap year…
  3. …unless it is also divisible by 400. In that case, it is a leap year.

Examples:

  • 2000 → leap year (divisible by 400)
  • 1900 → not a leap year (divisible by 100, but not 400)
  • 2024 → leap year (divisible by 4, not by 100)
  • 2023 → not a leap year

 

Learning Goals

By completing this problem, you should be able to:

  • Use modulus (<strong>%</strong>) to check divisibility.
  • Apply if / else if / else statements.
  • Write a program that gives clear output to the user.
  • Test your code with different inputs to confirm correctness.

 

Tasks

Task 1: Simple Divisibility by 4

  • Write a program that asks the user for a year.
  • If the year is divisible by 4, print "This is a leap year (maybe!)."
  • Otherwise, print "This is NOT a leap year."

 

Task 2: Add the 100-Year Rule

Update your program so that it:

  • Checks if the year is divisible by 100.
  • If it is divisible by 100 but not by 400, print "This is NOT a leap year."

 

Task 3: Add the 400-Year Rule

Update your program again:

  • If the year is divisible by 400, it is a leap year.
  • Otherwise, follow the earlier rules.

Now your program can correctly decide for any year.

 

Task 4: Test Your Program

Run your program with these years and record the results:

  1. 2000 → Leap year
  2. 1900 → Not a leap year
  3. 2020 → Leap year
  4. 2023 → Not a leap year
  5. 2400 → Leap year

 

Extension Challenge (Optional)

  • Update your program so that it keeps asking for years until the user types "quit".
  • Allow the user to enter multiple years at once (for example: 2000, 2023, 1900) and check each one.
  • Add a message that tells the user how many leap years they entered.

 

Success Criteria:

  • You can explain the rules of leap years.
  • Your program correctly determines if a year is a leap year.
  • Your program works for the provided test cases.
  • (Extension) Your program can handle multiple inputs and repeats until the user quits.