How to resolve the algorithm Find the last Sunday of each month step by step in the XPL0 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Find the last Sunday of each month step by step in the XPL0 programming language

Table of Contents

Problem Statement

Write a program or a script that returns the last Sundays of each month of a given year. The year may be given through any simple input method in your language (command line, std in, etc). Example of an expected output:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Find the last Sunday of each month step by step in the XPL0 programming language

Source code in the xpl0 programming language

func WeekDay(Year, Month, Day);   \Return day of week (0=Sun, 1=Mon ... 6=Sat)
int  Year, Month, Day;            \works for years from 1583 onward
[if Month<=2 then [Month:= Month+12;  Year:= Year-1];
return rem((Day-1 + (Month+1)*26/10 + Year + Year/4 + Year/100*6 + Year/400)/7);
];
 
int Year, Month, LastDay, WD;
[Year:= IntIn(8);               \from command line
for Month:= 1 to 12 do
    [LastDay:= WeekDay(Year, Month+1, 1) - WeekDay(Year, Month, 28);
    if LastDay < 0 then LastDay:= LastDay + 7;
    LastDay:= LastDay + 27;     \ = number of days in Month
    WD:= WeekDay(Year, Month, LastDay);
    LastDay:= LastDay - WD;
    IntOut(0, Year);  ChOut(0, ^-);
    if Month < 10 then ChOut(0, ^0);  IntOut(0, Month);  ChOut(0, ^-);
    IntOut(0, LastDay);  CrLf(0);
    ];
]

  

You may also check:How to resolve the algorithm Modular inverse step by step in the BCPL programming language
You may also check:How to resolve the algorithm Gaussian elimination step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Respond to an unknown method call step by step in the Smalltalk programming language
You may also check:How to resolve the algorithm Determine if a string has all unique characters step by step in the Racket programming language
You may also check:How to resolve the algorithm Atomic updates step by step in the Smalltalk programming language