How to get the number of days in a given month of a given year with Pascal

1 Answer

0 votes
program DaysInMonth;

function IsLeapYear(year: Integer): Boolean;
begin
    IsLeapYear := (year mod 4 = 0) and ((year mod 100 <> 0) or (year mod 400 = 0));
end;

function DaysInMonth(year, month: Integer): Integer;
const
    Days: array[1..12] of Integer = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
begin
    if (month = 2) and IsLeapYear(year) then
        DaysInMonth := 29
    else
        DaysInMonth := Days[month];
end;

var
    year, month: Integer;
begin
    year := 2024;
    month := 2;
    WriteLn('Days in month: ', DaysInMonth(year, month));
  
    year := 2025;
    month := 1;
    WriteLn('Days in month: ', DaysInMonth(year, month));
  
    year := 2025;
    month := 2;
    WriteLn('Days in month: ', DaysInMonth(year, month));
  
    year := 2025;
    month := 4;
    WriteLn('Days in month: ', DaysInMonth(year, month));
end.



(*
run:

Days in month: 29
Days in month: 31
Days in month: 28
Days in month: 30

*)

 



answered Feb 19, 2025 by avibootz

Related questions

...