How to print every month in a specified year that contains five full weekends (Fri, Sat, Sun) in Kotlin

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.

import java.time.DayOfWeek
import java.time.LocalDate

// ------------------------------------------------------------
// Month names as a single reusable constant
// ------------------------------------------------------------
val monthNames = listOf(
    "January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December"
)

// ------------------------------------------------------------
// Function: returns true if a month has 5 full weekends
// ------------------------------------------------------------
fun hasFiveFullWeekends(year: Int, month: Int): Boolean {
    val firstDay = LocalDate.of(year, month, 1)

    val daysInMonth = firstDay.lengthOfMonth()
    val weekday = firstDay.dayOfWeek

    return (daysInMonth == 31 && weekday == DayOfWeek.FRIDAY)
}

// ------------------------------------------------------------
// Main program
// ------------------------------------------------------------
fun main() {
    val yValue = 2026

    for (m in 1..12) {
        if (hasFiveFullWeekends(yValue, m)) {
            println("${monthNames[m - 1]} $yValue has five full weekends.")
        }
    }
}


/*
run:

May 2026 has five full weekends.

*/

 



answered 12 hours ago by avibootz

Related questions

...