Hi.
A few weeks a go a post some code to calculate the day of the week of a particular date. It was just an idea. I had to sit down and code it. Here you have it:
Code:
#include <stdio.h>
char *day[] = {"Friday","Saturday","Sunday","Monday","Tuesday","Wednesday",
"Thursday"};
int day_of_month[] = {31,28,31,30,31,30,31,31,30,31,30,31};
int day_of_week(int, int, int);
int leap (int);
void instructions(void);
int d, m, a;
int main () {
instructions();
do {
printf("\nEnter date -> ");
scanf("%d%d%d",&d, &m, &a);
printf("%d %d %d\n", d, m, a);
printf ("\nThe day of the week is %s \n", day[day_of_week(d,m,a)]);
} while ( (d!=0) && (m!=0) && (a!=0) );
}
int day_of_week(int d, int m, int a) {
int xa, i;
int days_counter = 0;
// Compute days from begining of era to december 31 of year a-1
for (xa = 0; xa < a; xa++)
if (leap(xa))
days_counter += 366;
else days_counter += 365;
// Compute days from months before m
for (i=0; (i+1) < m; i++)
days_counter += day_of_month[i];
// compute the remaining days
days_counter+= d;
// See if a is leap year and if so, see if date is after february
// Add 1 if so.
if ( (m > 2) && (leap(a)))
days_counter += 1;
return ( (int) (days_counter % 7) ); // return the remainder
}
int leap(int x) {
if ( (x % 400) == 0 ) // is leap
return (1);
else
if ( (x % 100) == 0 ) // it's not leap
return (0);
else if ( (x % 4) == 0 )
return (1); // is leap
else return (0); // it's not leap
}
void instructions(void) {
printf("This program computes the day of the week of a particular\n");
printf("date. The date must be entered in the form dd mm yyyy.\n");
printf("It should be separated by spaces. Example October 12 1492,\n");
printf("should be entered as : 12 10 1492 .\n");
printf("1. the program will give an answer even if the date does not exist.\n");
printf("2. the program only works for dates after Christ.\n");
printf("3. try the day you were born!\n");
}