Lesson 2.5 · Write it
Leap year check
A whole programPython or Java7 tests, 4 of them hidden
On a phone? Put the lines in order instead
Write a program that reads a year (a whole number, 1 or more) and checks whether it is a leap year.
- Always print
Year:followed by the year on the first line. - A year is a leap year if it is divisible by 4 (its remainder after dividing by 4 is 0).
- If it is a leap year, print
Leap yearon the next line. Otherwise, printNot a leap year. - Spaces at the end of a line are ignored.
This is a simplified rule. The real rule also checks 100 and 400, but this exercise uses only 4.
Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.
Examples
Your program is run 7 times: the 3 examples below, and 4 hidden ones that check the rules above. Only what it prints is checked.
divisible by 4: a leap year (rules 1 to 3)
You type
- typed: 2024
It prints
Year: 2024 Leap year
not divisible by 4 (rules 1 and 3)
You type
- typed: 2023
It prints
Year: 2023 Not a leap year
the smallest leap year above 0 (rule 2)
You type
- typed: 4
It prints
Year: 4 Leap year