Task

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



See also

 Leap year (wiki)


 
BEGIN
 
% COMPUTE P MOD Q %
INTEGER FUNCTION MOD(P, Q);
INTEGER P, Q;
BEGIN
  MOD := P - Q * (P / Q);
END;
 
% RETURN 1 IF Y IS A LEAP YEAR, OTHERWISE 0 %
INTEGER FUNCTION ISLEAP(Y);
INTEGER Y;
BEGIN
  IF MOD(Y,4) <> 0 THEN    % QUICK EXIT IN MOST CASES %
     ISLEAP := 0
  ELSE IF MOD(Y,400) = 0 THEN
     ISLEAP := 1
  ELSE IF MOD(Y,100) = 0 THEN
     ISLEAP := 0
  ELSE                     % NON-CENTURY DIVISIBLE BY 4 %
     ISLEAP := 1;
END;
 
% EXERCISE THE FUNCTION %
INTEGER Y;
WRITE("TEST OF CENTURY YEARS");
FOR Y := 1600 STEP 100 UNTIL 2000 DO
  BEGIN
    IF ISLEAP(Y) <> 0 THEN
      WRITE(Y, " IS A LEAP YEAR")
    ELSE
      WRITE(Y, " IS NOT A LEAP YEAR");
  END;
WRITE("TEST OF CURRENT DECADE");
FOR Y := 2010 STEP 1 UNTIL 2020 DO
  BEGIN
    IF ISLEAP(Y) <> 0 THEN
      WRITE(Y, " IS A LEAP YEAR")
    ELSE
      WRITE(Y, " IS NOT A LEAP YEAR");
  END;
 
END
 