How to print every month between two years that contains five full weekends (Fri, Sat, Sun) in Pascal

1 Answer

0 votes
// A month has five full weekends when it has 31 days,
// and the 1st day of the month is a Friday.

program FiveFullWeekends;

{$mode objfpc}

uses
  SysUtils;

// ------------------------------------------------------------
// Month names as a single reusable constant
// ------------------------------------------------------------
const
  monthNames: array[1..12] of string = (
    'January', 'February', 'March', 'April', 'May', 'June',
    'July', 'August', 'September', 'October', 'November', 'December'
  );

// ------------------------------------------------------------
// Helper: check if a year is a leap year
// ------------------------------------------------------------
function IsLeapYear(y: Integer): Boolean;
begin
  Result := ((y mod 4 = 0) and (y mod 100 <> 0)) or (y mod 400 = 0);
end;

// ------------------------------------------------------------
// Function: returns true if a month has 5 full weekends
// ------------------------------------------------------------
function hasFiveFullWeekends(y, m: Integer): Boolean;
var
  daysInMonth: Integer;
  weekday: Integer;
begin
  // Days in each month
  case m of
    1,3,5,7,8,10,12: daysInMonth := 31;
    4,6,9,11:        daysInMonth := 30;
    2: if IsLeapYear(y) then daysInMonth := 29 else daysInMonth := 28;
  end;

  // DayOfWeek: Sunday=1, Monday=2, ..., Friday=6, Saturday=7
  weekday := DayOfWeek(EncodeDate(y, m, 1));

  Result := (daysInMonth = 31) and (weekday = 6); // Friday
end;

// ------------------------------------------------------------
// Main program
// ------------------------------------------------------------
var
  startYear, endYear, y, m: Integer;
begin
  startYear := 2026;
  endYear   := 2030;

  for y := startYear to endYear do
  begin
    Write(y, ' ');

    for m := 1 to 12 do
    begin
      if hasFiveFullWeekends(y, m) then
        Write(#10, monthNames[m], ' ', y, ' has five full weekends.');
    end;

    Writeln;
  end;
end.



(* 
run:

2026 
May 2026 has five full weekends.
2027 
January 2027 has five full weekends.
October 2027 has five full weekends.
2028 
December 2028 has five full weekends.
2029 
2030 
March 2030 has five full weekends.

*)

 



answered 7 hours ago by avibootz

Related questions

...