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

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.

using System;

class FiveFullWeekends
{
    // ------------------------------------------------------------
    // Month names as a single reusable constant
    // ------------------------------------------------------------
    private static readonly string[] monthNames = {
        "January", "February", "March", "April", "May", "June",
        "July", "August", "September", "October", "November", "December"
    };

    // ------------------------------------------------------------
    // Function: returns true if a month has 5 full weekends
    // ------------------------------------------------------------
    static bool HasFiveFullWeekends(int year, int month)
    {
        DateTime firstDay = new DateTime(year, month, 1);

        int daysInMonth = DateTime.DaysInMonth(year, month);
        DayOfWeek wd = firstDay.DayOfWeek;

        return (daysInMonth == 31 && wd == DayOfWeek.Friday);
    }

    // ------------------------------------------------------------
    // Main program
    // ------------------------------------------------------------
    static void Main()
    {
        int y_value = 2026;

        for (int m = 1; m <= 12; m++) {
            if (HasFiveFullWeekends(y_value, m)) {
                Console.WriteLine($"{monthNames[m - 1]} {y_value} has five full weekends.");
            }
        }
    }
}



/*
run:

May 2026 has five full weekends.

*/

 



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

Related questions

...