How to resolve the algorithm Find the last Sunday of each month step by step in the Tcl 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 Tcl 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 Tcl programming language

Source code in the tcl programming language

proc lastSundays {{year ""}} {
    if {$year eq ""} {
	set year [clock format [clock seconds] -gmt 1 -format "%Y"]
    }
    foreach month {2 3 4 5 6 7 8 9 10 11 12 13} {
	set d [clock add [clock scan "$month/1/$year" -gmt 1] -1 day]
	while {[clock format $d -gmt 1 -format "%u"] != 7} {
	    set d [clock add $d -1 day]
	}
	lappend result [clock format $d -gmt 1 -format "%Y-%m-%d"]
    }
    return $result
}
puts [join [lastSundays {*}$argv] "\n"]


  

You may also check:How to resolve the algorithm String length step by step in the Lasso programming language
You may also check:How to resolve the algorithm Averages/Root mean square step by step in the Emacs Lisp programming language
You may also check:How to resolve the algorithm Blum integer step by step in the ALGOL 68 programming language
You may also check:How to resolve the algorithm Pi step by step in the C programming language
You may also check:How to resolve the algorithm Bitmap/Read a PPM file step by step in the ATS programming language