C Program to find day of birth from DOB

Problem Statement

Write a C program to find the day of the week from a date of birth. For example, given 02/04/2017 (2nd April 2017), the program should tell you it was a Sunday.

The Approach

  1. Pick a base year whose January 1st is a known weekday. We use 1900 (Jan 1, 1900 was a Monday).
  2. Count the years since the base year. Each ordinary year shifts the weekday by 1 (365 = 52×7 + 1), and each leap year adds an extra day.
  3. Add a precomputed month code, then the day of the month.
  4. Take the total modulo 7 — the remainder tells you the weekday.

The Program

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int d, m, y, year, month, day;

    printf("Enter date of birth (DD MM YYYY) : ");
    scanf("%d %d %d", &d, &m, &y);

    if ((d > 31) || (m > 12) || (y < 1900 || y >= 2100)) {
        printf("INVALID INPUT. Enter a valid date between 1900 and 2100.\n");
        exit(1);
    }

    year = y - 1900;
    year = year / 4;            /* number of leap years since 1900 */
    year = year + (y - 1900);   /* plus one shift per elapsed year */

    switch (m) {
    case 1:  case 10:           month = 1; break;
    case 2:  case 3:  case 11:  month = 4; break;
    case 4:  case 7:            month = 0; break;
    case 5:                     month = 2; break;
    case 6:                     month = 5; break;
    case 8:                     month = 3; break;
    case 9:  case 12:           month = 6; break;
    default:                    month = 0;
    }

    year = year + month + d;

    /* Leap-year correction for January AND February (before leap day). */
    if ((y > 1900) && (y % 4 == 0) && (m < 3))
        year--;

    day = year % 7;

    switch (day) {
    case 0: printf("Day is SATURDAY\n");  break;
    case 1: printf("Day is SUNDAY\n");    break;
    case 2: printf("Day is MONDAY\n");    break;
    case 3: printf("Day is TUESDAY\n");   break;
    case 4: printf("Day is WEDNESDAY\n"); break;
    case 5: printf("Day is THURSDAY\n");  break;
    case 6: printf("Day is FRIDAY\n");    break;
    }

    return 0;
}

How the Program Works

  • year = (y-1900)/4 counts the leap years since 1900, and adding (y-1900) accounts for the one-day shift every year contributes.
  • The switch assigns each month a code that compensates for the different number of days in earlier months.
  • Adding the day of the month and taking % 7 maps the total onto a weekday (0 = Saturday … 6 = Friday).
  • Bug fix: the original code corrected for leap years only when m < 2 (January), so February dates in leap years came out a day early. Using m < 3 fixes February too — for example Feb 29, 2016 now correctly reports Monday.

Sample Output

Enter date of birth (DD MM YYYY) : 02 04 2017
Day is SUNDAY

For the loops, arithmetic and control flow used here, The C Programming Language by Kernighan and Ritchie is the classic reference — find it on Amazon.

This post contains affiliate links. If you buy through them, we may earn a small commission at no extra cost to you.

Related C Programs

Try it instantly in one of the best online C compilers, or set up a local toolchain with our complete C development environment guide.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>