Ruby Program to Check Leap Year with Output and Explanation
In Ruby, you can check a leap year using
if (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0) to print whether the year is leap or not.Examples
Input2000
Output2000 is a leap year
Input1900
Output1900 is not a leap year
Input2024
Output2024 is a leap year
How to Think About It
To check if a year is leap, first see if it divides evenly by 400. If yes, it is leap. If not, check if it divides evenly by 4 but not by 100. If yes, it is leap. Otherwise, it is not leap.
Algorithm
1
Get the input year from the user2
Check if the year is divisible by 400; if yes, it is a leap year3
Else, check if the year is divisible by 4 but not by 100; if yes, it is a leap year4
Otherwise, it is not a leap year5
Print the resultCode
ruby
puts "Enter a year:" year = gets.to_i if (year % 400).zero? || ((year % 4).zero? && !(year % 100).zero?) puts "#{year} is a leap year" else puts "#{year} is not a leap year" end
Output
Enter a year:
2024
2024 is a leap year
Dry Run
Let's trace the year 2024 through the code
1
Input year
year = 2024
2
Check divisibility by 400
2024 % 400 = 24 (not zero)
3
Check divisibility by 4 and not by 100
2024 % 4 = 0 (zero), 2024 % 100 = 24 (not zero)
4
Determine leap year
Condition true, so 2024 is a leap year
| Year | year % 400 | year % 4 | year % 100 | Leap Year? |
|---|---|---|---|---|
| 2024 | 24 | 0 | 24 | Yes |
Why This Works
Step 1: Divisible by 400
A year divisible by 400 is always a leap year, so we check year % 400 == 0 first.
Step 2: Divisible by 4 but not 100
If not divisible by 400, a year divisible by 4 but not by 100 is a leap year, checked by year % 4 == 0 && year % 100 != 0.
Step 3: Otherwise not leap
If neither condition is true, the year is not a leap year.
Alternative Approaches
Using a method to return boolean
ruby
def leap_year?(year) (year % 400).zero? || ((year % 4).zero? && !(year % 100).zero?) end puts "Enter a year:" year = gets.to_i if leap_year?(year) puts "#{year} is a leap year" else puts "#{year} is not a leap year" end
This approach separates logic into a method for reuse and clarity.
Using case statement
ruby
puts "Enter a year:" year = gets.to_i case when (year % 400).zero? puts "#{year} is a leap year" when (year % 4).zero? && !(year % 100).zero? puts "#{year} is a leap year" else puts "#{year} is not a leap year" end
Using case without argument can make conditions clearer but is slightly longer.
Complexity: O(1) time, O(1) space
Time Complexity
The program performs a fixed number of arithmetic operations and checks, so it runs in constant time.
Space Complexity
Only a few variables are used, so the space used is constant.
Which Approach is Fastest?
All approaches run in constant time; using a method improves readability but does not affect speed.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Inline if-else | O(1) | O(1) | Simple scripts |
| Method returning boolean | O(1) | O(1) | Reusable code |
| Case statement | O(1) | O(1) | Clear condition grouping |
Use
.zero? method in Ruby to check if a number divides evenly by another.Beginners often forget to exclude years divisible by 100 but not by 400, causing incorrect leap year results.