Wednesday, June 28, 2006

LeapYear V2

And Neo said....'Hmmmm. Upgrades'.

Here is the version 2.0 of the program:

# LeapYearv2.rb - This program reads an year and tells whether it is a LeapYear or not.
# Author - Ashish Kulkarni (http://ashishkulkarni.blogspot.com)
# Date - 28-Jun-2006

GregorianCutoverYear = 1582

puts 'Please input the year for checking for Leap Year:'
STDOUT.flush
year = STDIN.gets.chomp.to_i

if (year >= GregorianCutoverYear && # Gregorian
((year.modulo(4) == 0) &&
((year.modulo(100) != 0) ||
(year.modulo(400) == 0)))) ||
(year < GregorianCutoverYear && # Julian
year.modulo(4) == 0)
puts 'It is a Leap Year'
else
puts 'It is not a Leap Year'
end


Output:

Please input the year for checking for Leap Year:
2000
It is a Leap Year

Please input the year for checking for Leap Year:
2002
It is not a Leap Year

Please input the year for checking for Leap Year:
2004
It is a Leap Year

Please input the year for checking for Leap Year:
2006
It is not a Leap Year

Please input the year for checking for Leap Year:
3000
It is not a Leap Year

1 Comments:

At 2:58 pm, Blogger raj said...

Approach 2 : I liked this one to swap the contents of two files.
I liked your approach from beginner's point of view i.e using function :
[ def filereadwrite(file1, file2)...... if f1 != nil then ........ ]
Thanks for your N approaches -- specially the "renaming " one looks bit funny but sure its thr some spicy brain,

 

Post a Comment

<< Home