-
Notifications
You must be signed in to change notification settings - Fork 0
/
leapyear.rb
34 lines (31 loc) · 919 Bytes
/
leapyear.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#Task: Write a program which will ask for a starting year and an ending year,
#and then puts all of the leap years between them (and including them,
#if they are also leap years). Leap years are years divisible by four (like 1984 and 2004).
#However, years divisible by 100 are not leap years (such as 1800 and 1900) unless they are divisible by 400.
puts "Starting year:"
year1 = gets.chomp.to_i
puts "Ending year:"
year2 = gets.chomp.to_i
if (year1 < year2)
counter = 0
year = year1
while year != year2+1
if year%4 == 0
if year%100 != 0
counter +=1
else
if year%400 == 0
counter +=1
end
end
end
year+=1
end
else
puts "Starting year has to be smaller than ending year"
end
if counter == 1
puts "There was 1 leap year between year #{year1} and #{year2}"
else
puts "There were #{counter} leap years between year #{year1} and #{year2}"
end