Using If-Then-Else

Δίσεκτο έτος
Δίσεκτο έτος στη διαδρομή Free Pascal
function LeapYear(const year : integer) : boolean;
begin
  if year mod 100 = 0 then
    result := year mod 400 = 0
  else
    result := year mod 4 = 0;
end;

Note that Pascal's if condition does not require surrounding parentheses — if year mod 100 = 0 then is valid, unlike C which requires if (year % 100 == 0).

The logic branches on divisibility by 100:

  • If year mod 100 = 0, the year is a century year, and is a leap year only if also divisible by 400.
  • Otherwise, divisibility by 4 is sufficient.

This order of checks uses at most two mod operations instead of up to three.

When both branches of an if/else are single statements, no begin/end block is needed.

Translation missing: el.number.nth.ordinalized Sep 2026 · Σου φάνηκε χρήσιμη;

Άλλες προσεγγίσεις για την άσκηση Δίσεκτο έτος στη διαδρομή Free Pascal

Άλλοι τρόποι με τους οποίους η κοινότητά μας έλυσε αυτή την άσκηση