Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,690 questions

55,449 answers

573 users

How to find the day of the week on any given date in C

1 Answer

0 votes
#include <stdio.h>

// Zeller's Congruence implementation
// Returns the day of the week for a given date.
const char* dayOfWeek(int d, int m, int y) {

    // Zeller's output mapping:
    // 0 = Saturday, 1 = Sunday, 2 = Monday, ... 6 = Friday
    const char* names[] = {
        "Saturday", "Sunday", "Monday", "Tuesday",
        "Wednesday", "Thursday", "Friday"
    };

    // In Zeller's formula, January and February are counted
    // as months 13 and 14 of the previous year.
    if (m < 3) {
        m += 12;   // Convert Jan → 13, Feb → 14
        y -= 1;    // Move to previous year
    }

    int K = y % 100;   // Year of the century (last two digits)
    int J = y / 100;   // Zero-based century (e.g., 2024 → 20)

    // Zeller's formula (clearer step-by-step version):

    int term1 = d;                     // day of month
    int term2 = (13 * (m + 1)) / 5;    // month adjustment
    int term3 = K;                     // year of century
    int term4 = K / 4;                 // leap years in century
    int term5 = J / 4;                 // leap centuries
    int term6 = 5 * J;                 // century correction

    // Combine all terms and take modulo 7
    int h = (term1 + term2 + term3 + term4 + term5 + term6) % 7;

    // Return the corresponding weekday name
    return names[h];
}

int main() {
    int d = 30, m = 5, y = 2024;

    printf("%s\n", dayOfWeek(d, m, y));

    return 0;
}


/*
run:

Thursday

*/

 



answered May 31 by avibootz
edited May 31 by avibootz
...