Home » Ruby » Ruby Programs

# 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.

What's New

- Business Environment MCQs
- Sustainable Development MCQs
- Marginal Costing and Absorption Costing MCQs
- Globalisation MCQs
- Indian Economy MCQs
- Retained Earnings MCQs
- Depreciation MCQs
- Partnership MCQs
- Sole Proprietorship MCQs
- Goods and Services Tax (GST) MCQs
- Cooperative Society MCQs
- Capital Market MCQs
- Business Studies MCQs
- Basic Accounting MCQs
- MIS Executive Interview Questions
- Go Language Interview Questions

Top Interview Coding Problems/Challenges!

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions!