Find the last Sunday of each month: Difference between revisions
Content added Content deleted
No edit summary |
(Added Kotlin) |
||
Line 1,944: | Line 1,944: | ||
Year> |
Year> |
||
</pre> |
|||
=={{header|Kotlin}}== |
|||
<lang scala>// version 1.0.6 |
|||
import java.util.Calendar |
|||
import java.util.GregorianCalendar |
|||
fun main(args: Array<String>) { |
|||
print("Enter a year : ") |
|||
val year = readLine()!!.toInt() |
|||
println("The last Sundays of each month in $year are as follows:") |
|||
val calendar = GregorianCalendar(year, 0, 31) |
|||
var daysInMonth: Int |
|||
var lastSunday: Int |
|||
for (month in 1..12) { |
|||
daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH) |
|||
lastSunday = daysInMonth - (calendar[Calendar.DAY_OF_WEEK] - Calendar.SUNDAY) |
|||
println("$year-" + "%02d-".format(month) + "%02d".format(lastSunday)) |
|||
if (month < 12) { |
|||
calendar.add(Calendar.DAY_OF_MONTH, 1) |
|||
calendar.add(Calendar.MONTH, 1) |
|||
calendar.add(Calendar.DAY_OF_MONTH, -1) |
|||
} |
|||
} |
|||
}</lang> |
|||
Sample input/output: |
|||
{{out}} |
|||
<pre> |
|||
Enter a year : 2013 |
|||
The last Sundays of each month in 2013 are as follows: |
|||
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 |
|||
</pre> |
</pre> |
||