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

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.{LocalDate, DayOfWeek}

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

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

  val daysInMonth: Int = firstDay.lengthOfMonth()
  val weekday: DayOfWeek = firstDay.getDayOfWeek()

  daysInMonth == 31 && weekday == DayOfWeek.FRIDAY
}

// ------------------------------------------------------------
// Main program
// ------------------------------------------------------------
@main def main(): Unit = {
  val yValue: Int = 2026

  for (m <- 1 to 12) {
    if (hasFiveFullWeekends(yValue, m)) {
      println(s"${monthNames(m - 1)} $yValue has five full weekends.")
    }
  }
}



/*
run:

May 2026 has five full weekends.

*/

 



answered May 27 by avibootz

Related questions

...