1. Is Leap Year Again

> Is a year a leap year?  The following Euphoria
> function will tell you.
>
> I converted the original C routine to Euphoria.
> It is interesting to note that the year 2000 will
> be a leap year.
>
> Larry Gregg

Ah, simple minded conversion yielded many "if", "end if" pairs.
I have replaced them with elsif, for elegance.

-- SNIP ----------------------------------------------------------------

global function isyearleap(integer year)
--  Returns 1 if year is a leap year, 0 otherwise.
--  Source:  "Practical Algorithms for Programmers"
--  By:  Andrew Binstock and John Rex

   if not remainder(year,4) = 0 then       --  If year not divisible by 4
      return 0                             --  it's not leap.
   elsif year < 1582 then                  --  All years divisible by 4 were
      return 1                             --  leap prior to 1582.
   elsif remainder(year,100) != 0 then     --  If year divisible by 4,
      return 1                             --  but not by 100, its leap.
   elsif not remainder(year,400) = 0 then  --  If year divisible by 100,
      return 0                             --  but not by 400, it's not leap.
   else
      return 1                             --  If divisible by 400, it's leap.
   end if
end function


-- SNIP ----------------------------------------------------------------
--  And to exercise the function:

integer year, rc

year = 1581
rc = isyearleap(year)
printf(1,"\nYear=%4d, is leap year? %1d",year&rc)

year = 1582
rc = isyearleap(year)
printf(1,"\nYear=%4d, is leap year? %1d",year&rc)

year = 1949
rc = isyearleap(year)
printf(1,"\nYear=%4d, is leap year? %1d",year&rc)

year = 1964
rc = isyearleap(year)
printf(1,"\nYear=%4d, is leap year? %1d",year&rc)

for i = 1200 to 2100 by 100 do
   year = i
   rc = isyearleap(year)
   printf(1,"\nYear=%4d, is leap year? %1d",year&rc)
end for

new topic     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu