From: HB Date: 2007-09-01T23:35:04+09:00 Subject: Help with leap year programing Hi, All, I am a beginner on programming now reading books by Chris Pine: Learn to Program. On chapter 6, I got same assignment. I guess that is a classic one. I have tried to use what I have learned so far: The loop. My code is as below. i have run several test without finding any problem. Can you help to have a look and throw some light on any possible improvement? Thanks. ____________________ puts 'starting year:' s = gets.chop puts 'ending year:' e = gets.chop if s.to_i > e.to_i puts 'ending year should be bigger than staring year' else puts 'leap year between ' + s + ' and '+ e + ' as below:' end while s.to_i < e.to_i while ( (s.to_i % 4 == 0 and s.to_i % 100 != 0) or (s.to_i % 100 == 0 and s.to_i % 400 == 0 )) puts s s = s.to_i + 1 end s = s.to_i + 1 end puts 'all done' On Nov 7, 2006, at 12:15 PM, Shiloh Madsen wrote: > So, I'm trying to go through the Teach Yourself Programming book by > Pragmatic Press and I am encountering a few hurdles. The chapter I am > working on now is asking me to create a program which will ask for a > start and end year and then calculate all leap years in that range. > The logic behind leap years (for those who need a refresher) is all > years divisible by for are leap years EXCEPT those that are divisible > by 100 UNLESS they are also divisible by 400. I am somewhat at a loss > for how to handle the logic for this...finding all numbers that are > divisible by 4 and removing those divisible by 100 is easy. Its adding > in that third condition which adds some of the removed numbers back > into the "true" group that I am having trouble with...or maybe I am > just not wrapping my mind around the problem well > enough...suggestions? The key point of all the methods proposed in this thread is: deal with the years divisible by 400 first, the years divisible by 100 second, and the years divisible by 4 last of all. Regards, Morton