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.

2026년 09월 23일 · 유용했나요?

Free Pascal의 윤년에 대한 다른 접근법

우리 커뮤니티가 이 연습 문제를 해결한 다른 방법