suse leap_Ruby程序检查leap年

suse leap

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

问题陈述 :给定年份,我们必须使用Ruby程序检查是否为Le年。

Methods used:

使用的方法:

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

    gets():此方法是String类的公共实例方法,用于从控制台获取输入。

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

    puts():此方法是String类的公共实例方法,用于在控制台上打印String。

Program 1:

程序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.

在上面的代码中,您可以观察到,首先,我们检查了年份是否可以被4整除,如果可以被4整除,那么该年份就是a年。 否则,要成为a年,它应该同时满足elsif中存在的两个条件,然后才将其视为a年。

Program 2:

程式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.

在上面的代码中,您可以观察到我们的程序正在寻找一个范围,在该范围内我们想找到the年。 您会看到,我们获得了介于范围之间的所有the年。

翻译自: https://www.includehelp.com/ruby/program-to-check-leap-year.aspx

suse leap

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值