This one is stumping me because of all the variables needed. Only thing

taught before this challenge was basic branches and loops. This is what

I have so far.

puts ‘Leap Year Calculator’

puts ’ ’

puts ‘Enter the starting year.’

starting_year = gets.chomp

puts ‘Enter the ending year.’

ending_year = gets.chomp

while true

if starting_year % 400 ||starting_year % 4

leap_year = starting_year

elsif starting_year % 100

leap_year = starting_year + 4

else

# need something to check until it reaches / by 4 or 400, adding one

each time maybe.

end

while leap_year.to_i < ending_year.to_i

puts leap_year

leap_year = leap_year.to_i + 4

end

break

end

Works okay if I put a leap year, or one dividable by 100. But I’m not

sure how to do this when I enter a number not dividable by 100, 4, or

400 (Where I have the comment is where I think the code needs to go.)

Also what I’m using is all the book has gone over so far so please,

nothing not using what I’m using. Thanks in advance.

On Thu, Mar 28, 2013 at 1:17 PM, Phil H. [email protected] wrote:

The rest doesn’t work at all because if you put something like 2096-2104

you would print out 2096, 2100 which is incorrect in two ways

- 2100 isn’t a leap year
- 2104 should be printed and your second while clause precludes that

You shouldn’t be worrying about the starting year as much as you are -

you

need to handle every leap year in the same fashion

So lets try something like this

leap_year = starting_year + starting_year % 4 #set yourself onto a

possible

leap year to start with

while leap_year <= ending_year

if (leap_year % 100) != 0 || (leap_year % 400) == 0

puts leap_year

end

leap_year = leap_year + 4

end

And if for some reason you have a problem with the or statement ("||")

you

can use this instead

if (leap_year % 100) != 0

puts leap_year

elsif (leap_year % 400) == 0

puts leap_year

end

John

I’m a bit depressed that worked so easily

I just could not come to that simple conclusion. Yes we did learn about

OR, AND, and one other that escapes me. I though about that as well.

Also setting the leap year ,it wouldn’t work unless I implicitly set it

to an integer. So my final code is:

puts ‘Leap Year Calculator’

puts ’ ’

puts ‘Enter the starting year.’

starting_year = gets.chomp

puts ‘Enter the ending year.’

ending_year = gets.chomp

leap_year = starting_year.to_i + starting_year.to_i % 4

while leap_year.to_i <= ending_year.to_i

if (leap_year % 100) != 0 || (leap_year % 400) == 0

puts leap_year

end

leap_year = leap_year.to_i + 4

end

Sobering

Am 28.03.2013 21:48, schrieb John W Higgins:

```
puts ' '
```

- 2104 should be printed and your second while clause precludes that

You shouldn’t be worrying about the starting year as much as you are -

you need to handle every leap year in the same fashion

So lets try something like this

leap_year = starting_year + starting_year % 4 #set yourself onto a

possible leap year to start with

does not work:

(1…12).each {|year| puts year + year % 4 }

2

4

6

4

6

8

10

8

10

12

14

12

puts “start year”

year01 = gets.chomp.to_i

puts “end year”

year02 = gets.chomp.to_i

for num in (year01…year02)

if num % 4 == 0

puts num

elsif num % 100 == 0

elsif num % 400 == 0

puts num

end

end

I like Deep Dan’s solution, it is more elegant, but if you are starting

out in ruby and have got to this post from ‘learn to program’ following

Phil H’s example to the end is probably more instructive. My noob final

code is

puts “please enter your start year”

start_year = gets.chomp.to_i

puts “please enter you ending year”

end_year = gets.chomp.to_i

leap_year = start_year - start_year % 4

leap_year = leap_year + 4 unless start_year == leap_year

while leap_year <= end_year

if (leap_year % 100) != 0 || (leap_year % 400) == 0

puts leap_year

end

leap_year = leap_year + 4

end

def leap_year?(year)

(year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)

end

On Thu, Mar 28, 2013 at 3:05 PM, [email protected] wrote:

what

The rest doesn’t work at all because if you put something like 2096-2104

leap_year = starting_year + starting_year % 4 #set yourself onto a

possible leap year to start with

does not work:

Absolutely correct - stupid line by me

How about

sy = starting_year.to_i

then either of

leap_year = sy - sy % 4

leap_year = leap_year + 4 unless sy == leap_year

or a single line overly cute option

leap_year = sy - sy % 4 + 4 * (sy % 4 <=> 0) # I can’t think of another

way

to get the signum function in ruby

John

I solved it!

#################################################################

puts ‘Write the starting year’

startYear = gets.to_i

puts ‘Write the ending year’

endYear = gets.to_i

while startYear <= endYear

if ((startYear % 4 == 0) && (startYear % 100 != 0)) || (startYear %

400 == 0)

puts startYear.to_s

end

startYear = startYear + 1

end