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:

./last_sundays 2013
2013-01-27
2013-02-24
2013-03-31
2013-04-28
2013-05-26
2013-06-30
2013-07-28
2013-08-25
2013-09-29
2013-10-27
2013-11-24
2013-12-29


Related tasks
 Day of the week
 Five weekends
 Last Friday of each month


 
yyyy=2013: if yyyy<1901 or yyyy>2099 then end
nda$="Lsu"
print "The last Sundays of "; yyyy
for mm=1 to 12
    x=NthDayOfMonth(yyyy, mm, nda$)
    select case mm
        case  1: print "   January ";   x
        case  2: print "   February ";  x
        case  3: print "   March ";     x
        case  4: print "   April ";     x
        case  5: print "   May ";       x
        case  6: print "   June ";      x
        case  7: print "   July ";      x
        case  8: print "   August ";    x
        case  9: print "   September "; x
        case 10: print "   October ";   x
        case 11: print "   November ";  x
        case 12: print "   December ";  x
    end select
next mm
end
 
function NthDayOfMonth(yyyy, mm, nda$)
' nda$ is a two-part code. The first character, n, denotes
' first, second, third, fourth, and last by 1, 2, 3, 4, or L.
' The last two characters, da, denote the day of the week by
' mo, tu, we, th, fr, sa, or su. For example: 
' the nda$ for the second Monday of a month is "2mo";
' the nda$ for the last Thursday of a month is "Lth".
    if yyyy<1900 or yyyy>2099 or mm<1 or mm>12 then
        NthDayOfMonth=0: exit function
    end if
    nda$=lower$(trim$(nda$))
    if len(nda$)<>3 then NthDayOfMonth=0: exit function
    n$=left$(nda$,1): nC$="1234l"
    da$=right$(nda$,2): daC$="tuwethfrsasumotuwethfrsasumo"
    if not(instr(nC$,n$)) or not(instr(daC$,da$)) then
        NthDayOfMonth=0: exit function
    end if
    NthDayOfMonth=1
    mm$=str$(mm): if mm<10 then mm$="0"+mm$ 
    db$=DayOfDate$(str$(yyyy)+mm$+"01")
    if da$<>db$ then
        x=instr(daC$,db$): y=instr(daC$,da$,x): NthDayOfMonth=1+(y-x)/2
    end if
    dim MD(12)
    MD(1)=31: MD(2)=28: MD(3)=31: MD(4)=30: MD(5)=31: MD(6)=30 
    MD(7)=31: MD(8)=31: MD(9)=30: MD(10)=31: MD(11)=30: MD(12)=31 
    if yyyy mod 4 = 0 then MD(2)=29
    if n$<>"1" then
        if n$<>"l" then
            NthDayOfMonth=NthDayOfMonth+((val(n$)-1)*7)
        else
            if NthDayOfMonth+27<MD(mm) then
                NthDayOfMonth=NthDayOfMonth+28
            else
                NthDayOfMonth=NthDayOfMonth+21
            end if
        end if
    end if
end function
 
function DayOfDate$(ObjectDate$) 'yyyymmdd format
    if ObjectDate$="" then 'today
        DaysSince1900 = date$("days")
    else
        DaysSince1900 = date$(mid$(ObjectDate$,5,2)+"/"+right$(ObjectDate$,2)_
                +"/"+left$(ObjectDate$,4))
    end if
    DayOfWeek = DaysSince1900 mod 7
    select case DayOfWeek
        case 0: DayOfDate$="tu"
        case 1: DayOfDate$="we"
        case 2: DayOfDate$="th"
        case 3: DayOfDate$="fr"
        case 4: DayOfDate$="sa"
        case 5: DayOfDate$="su"
        case 6: DayOfDate$="mo"
    end select
end function
 