Search⌘ K

Exercise 1: Check Leap Year

Explore how to define a Ruby method to check if a given year is a leap year using operator logic. Understand the rules for leap years and apply Ruby's operator behavior to solve this problem efficiently.

We'll cover the following...

Problem statement

Write a leap_year? method that takes a year (a number), and determines if it’s a leap year.

Example

leap_year?(2000)
leap_year?(2020)
leap_year?(2022)
leap_year?(2100)

This should print out:

true
true
false
false

Recall that a leap year meets two qualifications:

  • It’s divisible by four.
  • It’s not divisible by 100 unless it’s divisible by 400.

Note: You just need to provide the definition of the method, and it will be automatically evaluated on the given input value.

Try it yourself

Ruby
# Start your code here