So you want to find out which day of the week you were born. Well at least some of us do. I actually wrote this program because my whole family was born on Sundays, and my friends didn't believe it! An oldie but goodie, moved from DeSmet C to Turbo C and now to Pelles C. I just had somebody tell me that 01/01/1800 was a Wednesday, check it out with this program.
Day of week given a date
compzets commented: gud +0
// function to return the day of the week given the date
// (01/01/1800 was supposed to be a Wednesday)
// original Turbo C, modified for Pelles C by vegaseat 8oct2004
// Pelles C free at: http://smorgasbordet.com/pellesc/index.htm
#include <stdio.h> // for printf(), scanf(), getchar()
// years ending with 00 have to be divisible by 400 to leap
// note the "&&" is a DaniWeb problem and should be a double & for AND
#define isleapyear(year) ((!(year % 4) && (year % 100)) || (!(year % 400) && (year % 1000)))
int isdatevalid(int month, int day, int year);
int weekday(int month, int day, int year);
char week[7][10] = {
"Monday","Tuesday","Wednesday","Thursday",
"Friday","Saturday","Sunday"
};
int main()
{
int month, day, year;
printf("Return the day of the week given the date.");
printf("Enter date in the form mm/dd/yyyy : ");
scanf("%d/%d/%d",&month,&day,&year);
if (isdatevalid(month,day,year))
{
printf("The day of the week for this date is %s",
week[weekday(month,day,year)]);
}
else
printf("%d/%d/%d not a valid date!",
month,day,year);
getchar(); // wait
getchar(); // 2nd wait needed
return 0;
}
//
// return 1 if date is valid, 0 otherwise.
//
int isdatevalid(int month, int day, int year)
{
if (day <= 0) return 0 ;
switch( month )
{
case 1 :
case 3 :
case 5 :
case 7 :
case 8 :
case 10 :
case 12 : if (day > 31) return 0 ; else return 1 ;
case 4 :
case 6 :
case 9 :
case 11 : if (day > 30) return 0 ; else return 1 ;
case 2 :
if ( day > 29 ) return 0 ;
if ( day < 29 ) return 1 ;
if (isleapyear(year)) return 1 ; // leap year
else return 0 ;
}
return 0 ;
}
//
// given month, day, year, returns day of week, eg. Monday = 0 etc.
// tested for 1901 to 2099 (seems to work from 1800 on too)
//
int weekday(int month, int day, int year)
{
int ix, tx, vx;
switch (month) {
case 2 :
case 6 : vx = 0; break;
case 8 : vx = 4; break;
case 10 : vx = 8; break;
case 9 :
case 12 : vx = 12; break;
case 3 :
case 11 : vx = 16; break;
case 1 :
case 5 : vx = 20; break;
case 4 :
case 7 : vx = 24; break;
}
if (year > 1900) // 1900 was not a leap year
year -= 1900;
ix = ((year - 21) % 28) + vx + (month > 2); // take care of February
tx = (ix + (ix / 4)) % 7 + day; // take care of leap year
return (tx % 7);
}
Narue 5,707 Bad Cop Team Colleague
indranil87 0 Newbie Poster
statistician 0 Newbie Poster
RounaqJJW 0 Newbie Poster
deceptikon 1,790 Code Sniper Team Colleague Featured Poster
Lucaci Andrew 140 Za s|n
yde 0 Newbie Poster
deceptikon 1,790 Code Sniper Team Colleague Featured Poster
vijayan121 1,152 Posting Virtuoso
Yrth 18 Newbie Poster
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague
jnawrocki -3 Light Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.