Skip to content
Programming Decision making
All classes Beginner About 12 min Premium

Is it a leap year?

Choose a year and run a program that decides whether February has 29 days. See how the remainder operator % tests divisibility, and how AND and OR join three rules into one condition that gets 1900 and 2000 right.

Core concept

A year is a leap year when it divides by 4 and not by 100, or when it divides by 400; the remainder % tests divisibility and AND / OR join the rules into one condition

Topic: Decision making

The Earth takes about 365 and a quarter days to go round the Sun, so the calendar adds an extra day, 29 February, almost every four years. The rule has three parts. A year is a leap year if it divides exactly by 4, except years that divide by 100, which are not, except years that divide by 400, which are. A program tests “divides exactly” with the remainder operator %: year % 4 == 0 means dividing by 4 leaves nothing over. The three parts join with AND and OR into one condition: A question with a yes or no answer that decides what the program does next. All key words: (year % 4 == 0 AND year % 100 != 0) OR year % 400 == 0.

Key words explained

What you will learn

  • Use the remainder operator % to test whether one number divides another.
  • Join conditions with AND and OR, and read the brackets.
  • Predict whether a year is a leap year, including the century years.
  • Find the test values that check every part of a rule.

Before you start

  • Know how to divide and find a remainder.

Materials

  • A notebook to write your predictions

Equipment

  • None

Procedure

  1. Run the program for 2024 and for 2023.
  2. Try 1900, a century year, and then 2000.
  3. Answer the practice challenges.
Premium experiment Create a free account