How to print every month in a specified year 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}

// ------------------------------------------------------------
// 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'
  );

// ------------------------------------------------------------
// Zeller's congruence (returns 0=Saturday, 1=Sunday, ..., 6=Friday)
// ------------------------------------------------------------
function WeekdayOfDate(Year, Month, Day: Integer): Integer;
var
  K, J, h: Integer;
begin
  if Month < 3 then
  begin
    Month := Month + 12;
    Year := Year - 1;
  end;

  K := Year mod 100;
  J := Year div 100;

  h := (Day + (13 * (Month + 1)) div 5 + K + (K div 4) +
       (J div 4) + 5 * J) mod 7;

  Result := h;  // 0=Saturday, 6=Friday
end;

// ------------------------------------------------------------
// Function: returns true if a month has 5 full weekends
// ------------------------------------------------------------
function hasFiveFullWeekends(y: Integer; m: Integer): Boolean;
var
  DaysInMonth: array[1..12] of Integer =
    (31,28,31,30,31,30,31,31,30,31,30,31);
  Leap: Boolean;
  days, wd: Integer;
begin
  // Adjust February for leap years
  Leap := (y mod 4 = 0) and ((y mod 100 <> 0) or (y mod 400 = 0));
  if Leap then
    DaysInMonth[2] := 29;

  days := DaysInMonth[m];

  // First day of the month
  wd := WeekdayOfDate(y, m, 1);

  // Zeller: Friday = 6
  Result := (days = 31) and (wd = 6);
end;

// ------------------------------------------------------------
// Main program
// ------------------------------------------------------------
var
  y_value: Integer;
  m: Integer;
begin
  y_value := 2026;

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



(*
run:

May 2026 has five full weekends.

*)

 



answered 1 day ago by avibootz
edited 1 day ago by avibootz

Related questions

...