How to resolve the algorithm Last Friday of each month step by step in the C# programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Last Friday of each month step by step in the C# programming language

Table of Contents

Problem Statement

Write a program or a script that returns the date of the last Fridays of each month of a given year. The year may be given through any simple input method in your language (command line, std in, etc).

Example of an expected output:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Last Friday of each month step by step in the C# programming language

The provided C# code defines a program that identifies and prints the last Fridays of a given year. It uses a combination of date manipulation and iteration techniques to achieve this. Here's a detailed explanation:

  1. Namespace and Class:

    • The code is defined within a namespace named RosettaCode.LastFridaysOfYear and a class named Program.
  2. LastFridaysOfYear Method:

    • This is a private static method that takes an integer parameter year representing the year for which the last Fridays are to be found.
    • It uses a loop to iterate through each month of the year (month from 1 to 12).
    • For each month, it calculates the last day of that month by creating a DateTime object for the first day of the next month and subtracting one day (date = new DateTime(year, month, 1).AddMonths(1).AddDays(-1)).
    • It then enters a while loop that keeps subtracting one day from the date until it falls on a Friday (date.DayOfWeek != DayOfWeek.Friday).
  3. Main Method:

    • This is the program's entry point.
    • It parses the first argument passed to the program as an integer, representing the year. If the argument is empty or cannot be parsed as an integer, it defaults to the current year (DateTime.Today.Year).
    • It calls the LastFridaysOfYear method with the specified or default year and iterates through the returned sequence of dates.
    • For each date, it prints it in the format "d" (e.g., "12/31/2023") using the invariant culture (ToString("d", CultureInfo.InvariantCulture)).
  4. Execution:

    • When you run the program, it will prompt you to enter a year (if no argument is provided), or take the year as an argument.
    • It will then print the last Fridays of the specified year, each on a separate line in the format "MM/dd/yyyy".

For example, if you input the year 2023, the program will print the following output:

03/31/2023
05/26/2023
07/28/2023
09/29/2023
11/24/2023

Source code in the csharp programming language

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace RosettaCode.LastFridaysOfYear
{
    internal static class Program
    {
        private static IEnumerable<DateTime> LastFridaysOfYear(int year)
        {
            for (var month = 1; month <= 12; month++)
            {
                var date = new DateTime(year, month, 1).AddMonths(1).AddDays(-1);
                while (date.DayOfWeek != DayOfWeek.Friday)
                {
                    date = date.AddDays(-1);
                }
                yield return date;
            }
        }

        private static void Main(string[] arguments)
        {
            int year;
            var argument = arguments.FirstOrDefault();
            if (string.IsNullOrEmpty(argument) || !int.TryParse(argument, out year))
            {
                year = DateTime.Today.Year;
            }

            foreach (var date in LastFridaysOfYear(year))
            {
                Console.WriteLine(date.ToString("d", CultureInfo.InvariantCulture));
            }
        }
    }
}


  

You may also check:How to resolve the algorithm Hello world/Newline omission step by step in the Falcon programming language
You may also check:How to resolve the algorithm Simple database step by step in the Tcl programming language
You may also check:How to resolve the algorithm Hello world/Newline omission step by step in the gecho programming language
You may also check:How to resolve the algorithm Scope modifiers step by step in the J programming language
You may also check:How to resolve the algorithm Floyd-Warshall algorithm step by step in the F# programming language