# Board index » delphi » leap year -mod

## leap year -mod

and didnt come across an example of how mod works.
This example contains some code from a book I own

can someone explain how one of these mod works in the code below ?
2000 might be a good year to use in the example
because it is a leapyear.

I also wanted to mention are all these else clauses
(except for the last one) really necessary?
couldnt you just keep using
if then
some code;
if then
some more code;

Thanks
jim

function date.leapyear: boolean;
begin
if (year mod 4 <> 0 ) then
leapyear:=false
else
if (year mod 100 <> 0) then
leapyear:=true
else
if  (year mod 400 <> 0) then
leapyear:=false

if (year mod 400 =0) then
leapyear:=true
else
if (year mod 100=0) then
leapyear:=false
else
leapyear:=true;
end;

## Re:leap year -mod

Jim,

From the delphi help:

The mod operator returns the remainder obtained by dividing its
operands. In other words, x mod y = x (x div y) * y.

In english, the mod operator returns the remainder after dividing by
y.   3 mod 4 = 3,  4 mod 4 = 0,  5 mod 4 =1.  Therefore mod is useful to
determine if one number is exactly divisible by another.

The rule for a leap year is:

Yes if divisible by 4, unless also divisible by 100 but yes if divisible
by 400.  Your coding example is not the best. I would use.

function leapyear(y:integer) : boolean;
begin
result = ((y mod 4=0) and (y mod 100<>0)) or (y mod 400=0);
end;

seems easier to me.

...Jim

##### Quote
Jim McNamara wrote:
> and didnt come across an example of how mod works.
> This example contains some code from a book I own

> can someone explain how one of these mod works in the code below ?
> 2000 might be a good year to use in the example
> because it is a leapyear.

> I also wanted to mention are all these else clauses
> (except for the last one) really necessary?
> couldnt you just keep using
> if then
> some code;
> if then
> some more code;

> Thanks
> jim

> function date.leapyear: boolean;
> begin
> if (year mod 4 <> 0 ) then
>  leapyear:=false
> else
>  if (year mod 100 <> 0) then
>   leapyear:=true
> else
>  if  (year mod 400 <> 0) then
> leapyear:=false

> if (year mod 400 =0) then
>  leapyear:=true
> else
>  if (year mod 100=0) then
> leapyear:=false
> else
> leapyear:=true;
> end;

## Re:leap year -mod

Dear Jim

A better way is to:

interface section
function ODIsLeapYear(Year:integer): Boolean;
function ODDaysThisMonth(Month,Year:integer): Integer;

implementation section
function ODIsLeapYear(Year:integer): Boolean;
begin
Result := (Year mod 4 = 0)    { years divisible by 4 are... }
and ((Year mod 100 <> 0)      { ...except century years... }
or (Year mod 400 = 0));     { ...unless it's divisible by 400 }
end;

function ODDaysThisMonth(Month,Year:integer): Integer;
const
Day{*word*237}onth: array[1..12] of Integer =
(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);   { usual numbers
of days }
begin
Result := Day{*word*237}onth[Month];        { normally, just return number }
if (Month = 2) and ODIsLeapYear(Year) then Inc(Result);       { plus 1 in
leap February }
end;

The mod function works by giving you the remainder of a number divided
by another number: eg: 15 mod 10 is 15 divided by 10 which is 1
remainder 5; therefore 5 is the answer.  Mod is useful if you have a
loop and want to do something like display where you are every 100th
iteration (eg: if ix mod 100 then StatusBar.SimpleText:='I am on
record '+inttostr(ix); application.processmessages).

To get the whole part of a number divided by another number, use the
div function.

Note DIV and MOD work for integers only.

On Tue, 14 Nov 2000 15:38:38 -0800, Jim McNamara <m...@slic.com>
wrote:

##### Quote
>and didnt come across an example of how mod works.
>This example contains some code from a book I own

>can someone explain how one of these mod works in the code below ?
>2000 might be a good year to use in the example
>because it is a leapyear.

>I also wanted to mention are all these else clauses
>(except for the last one) really necessary?
>couldnt you just keep using
>if then
>some code;
>if then
>some more code;

>Thanks
>jim

>function date.leapyear: boolean;
>begin
>if (year mod 4 <> 0 ) then
> leapyear:=false
>else
> if (year mod 100 <> 0) then
>  leapyear:=true
>else
> if  (year mod 400 <> 0) then
>leapyear:=false

>if (year mod 400 =0) then
> leapyear:=true
>else
> if (year mod 100=0) then
>leapyear:=false
>else
>leapyear:=true;
>end;

David