C/C++ program to determine the day of a week

This tutorial is about finding out the corresponding day of the given date. For example, if I want to know which is the corresponding day of date 1991 March 10, then the program returns “Sunday”. There are various algorithm proposed for this. The below program is based on and valid for

Source Code

#include <stdio.h>
 
char *getName(int day){ //returns the name of the day
   switch(day){
      case 0 :return("Sunday");
      case 1 :return("Monday");
      case 2 :return("Tuesday");
      case 3 :return("Wednesday");
      case 4 :return("Thursday");
      case 5 :return("Friday");
      case 6 :return("Saturday");
      default:return("Error: Invalid Argument Passed");
   }
}
 
int getDayNumber(int dd,int mm,int yy){ //retuns the day number
    static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
    yy -= mm < 3;
    return (yy + yy/4 - yy/100 + yy/400 + t[mm-1] + dd) % 7;
}
 
int main(){
    int dd, mm, yy;
    printf("Enter day month and year (dd mm yyyy): ");
    scanf("%d%d%d", &dd, &mm, &yy);
    printf("\n The Corresponding day is %s", getName(getDayNumber(dd, mm, yy)));
}

Output :

SHARE C/C++ program to determine the day of a week

You may also like...

2 Responses

  1. Anonymous says:

    Thank you sir for this program …. but i thought you must know that this program has a bug of giving out results of undefined dates, such as 29th of February 2015 (that doesnt even exits) so please reply if you can help me out.
    Thank you

  2. Write a program that tells the following about days of week.

    If day is from 1 – 5 then it is weekday

    If its 6th day (Saturday) then it is weekend

    If its 7th day (Sunday) then it is holiday.

Leave a Reply

Your email address will not be published.

Share