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

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.

Imports System

Module FiveFullWeekends

    ' ------------------------------------------------------------
    ' Month names as a single reusable constant
    ' ------------------------------------------------------------
    Private ReadOnly monthNames() As String = {
        "January", "February", "March", "April", "May", "June",
        "July", "August", "September", "October", "November", "December"
    }

    ' ------------------------------------------------------------
    ' Function: returns true if a month has 5 full weekends
    ' ------------------------------------------------------------
    Function HasFiveFullWeekends(year As Integer, month As Integer) As Boolean
        Dim firstDay As New DateTime(year, month, 1)

        Dim daysInMonth As Integer = DateTime.DaysInMonth(year, month)
        Dim wd As DayOfWeek = firstDay.DayOfWeek

        Return (daysInMonth = 31 And wd = DayOfWeek.Friday)
    End Function

    ' ------------------------------------------------------------
    ' Main program
    ' ------------------------------------------------------------
    Sub Main()
        Dim y_value As Integer = 2026

        For m As Integer = 1 To 12
            If HasFiveFullWeekends(y_value, m) Then
                Console.WriteLine(monthNames(m - 1) & " " & y_value &
                                  " has five full weekends.")
            End If
        Next
    End Sub

End Module

 
 
' run:
' 
' May 2026 has five full weekends.
'


 



answered 1 day ago by avibootz
edited 1 day ago by avibootz

Related questions

...