How to resolve the algorithm Day of the week step by step in the C programming language

Published on 7 June 2024 03:52 AM
#C

How to resolve the algorithm Day of the week step by step in the C programming language

Table of Contents

Problem Statement

A company decides that whenever Xmas falls on a Sunday they will give their workers all extra paid holidays so that, together with any public holidays, workers will not have to work the following week (between the 25th of December and the first of January).

In what years between 2008 and 2121 will the 25th of December be a Sunday? Using any standard date handling libraries of your programming language; compare the dates calculated with the output of other languages to discover any anomalies in the handling of dates which may be due to, for example, overflow in types used to represent dates/times similar to   y2k   type problems.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Day of the week step by step in the C programming language

Overview: The provided code snippet calculates the day of the week (Sunday = 0) for a given date using the proleptic Gregorian calendar. It then prints the dates when December 25th falls on a Sunday in the range of years 2008 to 2121.

Detailed Breakdown:

wday Function:

  • Calculates the day of the week for a given date using the proleptic Gregorian calendar.
  • It takes three arguments: year (y), month (m), and day (d).
  • It first calculates an adjustment based on the month, then adjusts the month (mm) and year (yy) accordingly.
  • It then uses Zeller's congruence formula to calculate the day of the week:
    • day + (13 * mm - 1) / 5 + yy + yy / 4 - yy / 100 + yy / 400
  • This formula takes into account the day, month, year, leap year, and century leap year adjustments.
  • The result is returned as the day of the week modulo 7 (Sunday = 0).

main Function:

  • Defines an integer variable y used to iterate through years.
  • Uses a for loop to iterate from year 2008 to 2121.
  • For each year, it calls the wday function to calculate the day of the week for December 25th.
  • If December 25th is a Sunday (day of week = 0), it prints the year in the format "YYYY-12-25".

Output: The code prints the following dates when December 25th falls on a Sunday in the range of years 2008 to 2121:

  • 2008-12-25
  • 2019-12-25

Source code in the c programming language

#include <stdio.h>

/* Calculate day of week in proleptic Gregorian calendar. Sunday == 0. */
int wday(int year, int month, int day)
{
	int adjustment, mm, yy;

	adjustment = (14 - month) / 12;
	mm = month + 12 * adjustment - 2;
	yy = year - adjustment;
	return (day + (13 * mm - 1) / 5 +
		yy + yy / 4 - yy / 100 + yy / 400) % 7;
}

int main()
{
	int y;

	for (y = 2008; y <= 2121; y++) {
		if (wday(y, 12, 25) == 0) printf("%04d-12-25\n", y);
	}

	return 0;
}


  

You may also check:How to resolve the algorithm Named parameters step by step in the C programming language
You may also check:How to resolve the algorithm Visualize a tree step by step in the Julia programming language
You may also check:How to resolve the algorithm Cullen and Woodall numbers step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm String interpolation (included) step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Sorting algorithms/Comb sort step by step in the Delphi programming language