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.
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
2012 is leap year 1500 is not leap year 1600 is leap year 2020 is leap year
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?
- New Content published on w3resource:
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework