[Eiffel Tips] Determine whether a given year is a leap year in the Gregorian calendar

in eiffel •  7 years ago  (edited)

Leap year

In this little blog will show you how to resolve the Leap year task of Rosetta Code, here it is the Eiffel solution.

What is a Leap Year?

A leap year (also known as an intercalary year or bissextile year) is a calendar year containing one additional day (or, in the case of lunisolar calendars, a month) added to keep the calendar year synchronized with the astronomical or seasonal year Wikipedia Leap year

Leap Year Program In Eiffel

The code below uses DATE class part of the Eiffel time library and the algorithm from the wiki is_leap_year in the code. Both values are printed in the output.

class
    LEAP_YEAR_EXAMPLE

create
    make

feature -- Initialization

    make
            -- Run example.
        local
            l_date: DATE
            l_dates: ARRAY [INTEGER]
        do
            l_dates :=  {ARRAY[INTEGER]}<<1800,1900,1994,1998,1999,2000,2001,2004,2100,2012,2016,1933>>
            create l_date.make_now
            across l_dates as ic loop
                print ("%NIs the year [" + ic.item.out + "] a leap year? " + l_date.is_leap_year (ic.item).out + " - " +                                             is_leap_year (ic.item).out)
            end
        end

    is_leap_year (a_year: INTEGER): BOOLEAN
            -- Is year `a_year' a leap year?
        do
            if a_year \\ 100 = 0 then
                Result := a_year \\ 400 = 0
            else
                Result := a_year \\ 4  = 0
            end
        end
end

Run the code and you will see the following output


Is the year [1800] a leap year? False - False
Is the year [1900] a leap year? False - False
Is the year [1994] a leap year? False - False
Is the year [1998] a leap year? False - False
Is the year [1999] a leap year? False - False
Is the year [2000] a leap year? True - True
Is the year [2001] a leap year? False - False
Is the year [2004] a leap year? True - True
Is the year [2100] a leap year? False - False
Is the year [2012] a leap year? True - True
Is the year [2016] a leap year? True - True
Is the year [1933] a leap year? False - False

Get the code here

  https://github.com/jvelilla/RosettaCode

You will find more examples in Eiffel based on problem descriptions from Rosetta Code

If you are new to Eiffel check the following links

On upcoming posts, I will show you how :

  • Build a REST API using EiffelWeb.
  • How to send emails
  • and more ...!
Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Congratulations @eiffel4all! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of upvotes received

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

Upvote this notification to help all Steemit users. Learn why here!

Congratulations @eiffel4all! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Do not miss the last post from @steemitboard:

The Steem blockchain survived its first virus plague!
Vote for @Steemitboard as a witness to get one more award and increased upvotes!