Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

Ruby Basic Exercises: Test whether a year is leap year or not

Ruby Basic: Exercise-33 with Solution

Write a Ruby program to test whether a year is leap year or not.

Ruby Basic Exercises: Test whether a year is leap year or not

Ruby Code:

year = [2012, 1500, 1600, 2020]
year.each do |y|
  if y % 400 == 0
  	 puts y.to_s + ' is leap year'
    elsif y % 4==0 && y % 100 != 0 
      puts y.to_s + ' is leap year'
  else  puts y.to_s + ' is not leap year'
  end
end

Output:

2012 is leap year
1500 is not leap year
1600 is leap year
2020 is leap year

Flowchart:

Flowchart: Test whether a year is leap year or not

Ruby Code Editor:

Contribute your code and comments through Disqus.

Previous:Write a Ruby program to print a specified character twenty times.
Next: Write a Ruby program to check whether a string 'Java' appears at index 1 in a given sting, if 'Java' appears return the string without 'Java' otherwise return the string unchanged.

What is the difficulty level of this exercise?