//------------------------------------------------------------------- // firstday.cpp // // This program shows which day of the week is January first in // any year from 1582 to 9999 given by a command-line argument. // // compile-and-link with: $ g++ firstday.cpp -o firstday // // programmer: ALLAN CRUSE // written on: 02 APR 2006 //------------------------------------------------------------------- #include // for printf(), fprintf() #include // for exit(), atoi() int main( int argc, char **argv ) { if ( argc == 1 ) { fprintf( stdout, "usage: $ firstday \n" ); exit(1); } int year = atoi( argv[1] ); if (( year < 1582 )||( year > 9999 )) { fprintf( stdout, "year must be from 1582 to 9999 \n" ); exit(1); } //--------------------------------------------------------------- // Here is our algorithm for computing which day is January first int years_since_1 = (year - 1)%400; int leaps_since_1 = years_since_1 / 4 - years_since_1 / 100; int january_first = (1 + years_since_1 + leaps_since_1) % 7; //--------------------------------------------------------------- char *day[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; printf( "\n\tYear %u begins %s \n\n", year, day[ january_first ] ); }