Cover Image for Find Day from Day in C without Using Function
82 views

Find Day from Day in C without Using Function

If you want to find the day of the week for a given date in C without using any external libraries or functions, you can implement a basic algorithm based on the Zeller’s Congruence formula. Zeller’s Congruence is a mathematical formula used to calculate the day of the week for a given date. Here’s a C program that does this:

C
#include <stdio.h>

int main() {
    int day, month, year;

    printf("Enter date (day month year): ");
    scanf("%d %d %d", &day, &month, &year);

    // Adjust the month and year for the formula
    if (month < 3) {
        month += 12;
        year--;
    }

    int dayOfWeek;

    // Zeller's Congruence formula
    dayOfWeek = (day + (13 * (month + 1)) / 5 + year + (year / 4) - (year / 100) + (year / 400)) % 7;

    // Define the days of the week
    char* days[] = { "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };

    printf("The day of the week for %d-%02d-%02d is %s.\n", year, month, day, days[dayOfWeek]);

    return 0;
}

In this program:

  1. We take input for the day, month, and year.
  2. We adjust the month and year to match the requirements of Zeller’s Congruence. In this formula, January and February are counted as months 13 and 14 of the previous year.
  3. We calculate the day of the week using Zeller’s Congruence formula and store it in the dayOfWeek variable.
  4. We define an array days to map the numeric result to the corresponding day of the week.
  5. Finally, we print the result based on the calculated day of the week.

This program calculates and displays the day of the week for the given date without using any external functions or libraries.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS