Task

Determine whether a given year is a leap year in the Gregorian calendar.



See also

 Leap year (wiki)


window 1
 
// In-line C function to generate random number in range
BeginCFunction
  long randomInRange( long min, long max ) {
  int i = (arc4random()%(max-min+1))+min;
  return (long)i; 
  }
EndC
toolbox fn randomInRange( long min, long max  ) = long
 
// Leap year test function
local fn LeapYear( year as long ) as BOOL
  BOOL result : result = _false
 
  if year mod 400 == 0 then result = _true  : exit fn
  if year mod 100 == 0 then result = _false : exit fn
  if year mod   4 == 0 then result = _true  : exit fn
  if year mod   4 != 0 then result = _false : exit fn
end fn = result
 
long i, y, knownLeapYear(10)
 
// Array of known leap years from 1980 through 2020 for control
knownLeapYear(0) = 1980 : knownLeapYear(1)  = 1984 : knownLeapYear(2) = 1988
knownLeapYear(3) = 1992 : knownLeapYear(4)  = 1996 : knownLeapYear(5) = 2000
knownLeapYear(6) = 2004 : knownLeapYear(7)  = 2008 : knownLeapYear(8) = 2012
knownLeapYear(9) = 2016 : knownLeapYear(10) = 2020
 
print "Known leap years:"
for i = 0 to 9
  if ( fn LeapYear( knownLeapYear(i) ) == _true )
    print knownLeapYear(i); " is a leap year."
  else
    print knownLeapYear(i); " is a not leap year."
  end if
next
 
print
 
// Random years from 1980 to 2020 to test
print "Check random years:"
for i = 0 to 20
  y = fn randomInRange( 1980, 2020  )
  if ( fn LeapYear( y ) == _true )
    print y; " is a leap year."
  else
    print y; " is a not leap year."
  end if
next
 
HandleEvents