Ruby program to check leap year

Ruby | Checking Leap Year: Here, we are going to learn how to check whether a given year is Leap year or not?
Submitted by Hrithik Chandra Prasad, on May 07, 2020

Problem statement: Given a year, we have to check whether it is a Leap year or not using Ruby program.

Methods used:

  • gets(): This method is a public instance method of String class which is used to take input from the console.
  • puts(): This method is a public instance method of String class which is used to print Strings on the console.

Program 1:

=begin
  Ruby program to check whether 
  the year is leap year or not.	
=end

puts "Enter the year you want to check"
yr = gets.chomp.to_i

if yr % 400 == 0
	puts "#{yr} is a leap year"
elsif yr % 4 == 0 && yr % 100 !=0
	puts "#{yr} is a leap year"
else
	puts "#{yr} is not a leap year"
end

Output

RUN 1:
Enter the year you want to check
 2020
2020 is a leap year

RUN 2:
Enter the year you want to check
 1900
1900 is not a leap year

RUN 3:
Enter the year you want to check
 2200
2200 is not a leap year

RUN 4:
Enter the year you want to check
 2204
2204 is a leap year

Explanation:

In the above code, you can observe that first, we have checked whether the year is divisible by 4 or not, if it is divisible by 4 then the year is simply a leap year. Otherwise, to be a leap year, it should satisfy both the conditions which are there in elsif then only it will be considered as a leap year otherwise not.

Program 2:

=begin
  Ruby program to check whether the 
  year is leap year or not between range.		
=end

puts "Enter the lower limit:"
lwr = gets.chomp.to_i
puts "Enter the upper limit:"
upr = gets.chomp.to_i

for yr in lwr..upr do
  if yr % 400 == 0
    puts "#{yr} is a leap year"
  elsif yr % 4 == 0 && yr % 100 !=0
    puts "#{yr} is a leap year"
  else
    puts "#{yr} is not a leap year"
  end
end

Output

Enter the lower limit:
 2000
Enter the upper limit:
 2100
2000 is a leap year
2001 is not a leap year
2002 is not a leap year
2003 is not a leap year
2004 is a leap year
2005 is not a leap year
2006 is not a leap year
2007 is not a leap year
2008 is a leap year
2009 is not a leap year
2010 is not a leap year
2011 is not a leap year
2012 is a leap year
2013 is not a leap year
2014 is not a leap year
2015 is not a leap year
2016 is a leap year
2017 is not a leap year
2018 is not a leap year
2019 is not a leap year
2020 is a leap year
2021 is not a leap year
2022 is not a leap year
2023 is not a leap year
2024 is a leap year
2025 is not a leap year
2026 is not a leap year
2027 is not a leap year
2028 is a leap year
2029 is not a leap year
2030 is not a leap year
2031 is not a leap year
2032 is a leap year
2033 is not a leap year
2034 is not a leap year
2035 is not a leap year
2036 is a leap year
2037 is not a leap year
2038 is not a leap year
2039 is not a leap year
2040 is a leap year
2041 is not a leap year
2042 is not a leap year
2043 is not a leap year
2044 is a leap year
2045 is not a leap year
2046 is not a leap year
2047 is not a leap year
2048 is a leap year
2049 is not a leap year
2050 is not a leap year
2051 is not a leap year
2052 is a leap year
2053 is not a leap year
2054 is not a leap year
2055 is not a leap year
2056 is a leap year
2057 is not a leap year
2058 is not a leap year
2059 is not a leap year
2060 is a leap year
2061 is not a leap year
2062 is not a leap year
2063 is not a leap year
2064 is a leap year
2065 is not a leap year
2066 is not a leap year
2067 is not a leap year
2068 is a leap year
2069 is not a leap year
2070 is not a leap year
2071 is not a leap year
2072 is a leap year
2073 is not a leap year
2074 is not a leap year
2075 is not a leap year
2076 is a leap year
2077 is not a leap year
2078 is not a leap year
2079 is not a leap year
2080 is a leap year
2081 is not a leap year
2082 is not a leap year
2083 is not a leap year
2084 is a leap year
2085 is not a leap year
2086 is not a leap year
2087 is not a leap year
2088 is a leap year
2089 is not a leap year
2090 is not a leap year
2091 is not a leap year
2092 is a leap year
2093 is not a leap year
2094 is not a leap year
2095 is not a leap year
2096 is a leap year
2097 is not a leap year
2098 is not a leap year
2099 is not a leap year
2100 is not a leap year

Explanation:

In the above code, you can observe that our program is asking for a range under which we want to find the leap year. You can see that we are getting all the leap years which lie between the range.

Ruby Basic Programs »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.