/*Given any date find the day in Calender.*/
  #include 

int main(void)
{
int dd, mm, yy, mmdays, dddays, leapdays, dayno, i;
long int yydays, totdays;

printf( "nEnter Date ( dd mm yy ) : " );
scanf( "%d%d%d", &dd, &mm, &yy );

/* Step 1 : Calculate days due to elapsed years */
yydays = (yy - 1) * 365L;

/* Step 2 : Calculate days due to elapsed months */
switch(mm)
{
case 1: mmdays = 0; break;
case 2: mmdays = 31; break;
case 3: mmdays = 31+28; break;
case 4: mmdays = 31+28+31; break;
case 5: mmdays = 31+28+31+30; break;
case 6: mmdays = 31+28+31+30+31; break;
case 7: mmdays = 31+28+31+30+31+30; break;
case 8: mmdays = 31+28+31+30+31+30+31; break;
case 9: mmdays = 31+28+31+30+31+30+31+31; break;
case 10: mmdays = 31+28+31+30+31+30+31+31+30; break;
case 11: mmdays = 31+28+31+30+31+30+31+31+30+31; break;
case 12: mmdays = 31+28+31+30+31+30+31+31+30+31+30; break;
}

/* Step 3 : Calculate days due to elapsed date */
dddays = dd - 1 ;

/* Step 4 : Account for leap days */
/* Step 4a : Leap days due to elapsed years */
/* leapdays = (yy-1)/4 - (yy-1)/100 + (yy-1)/400 ; */

for(i = 1, leapdays = 0 ; i < yy ; i++ )
if( i % 400 == 0 || (i % 100 != 0 && i % 4 == 0 ) )
leapdays++;

/* Step 4b : Leap day due to present year */
if( yy % 400 == 0 || ( yy % 100 != 0 && yy % 4 == 0 ) )
leapdays = leapdays + ( mm > 2 ? 1 : 0 );

totdays = yydays+mmdays+dddays+leapdays ;
dayno = totdays % 7;
switch(dayno)
{
case 0 : printf( "nMonday" ); break;
case 1 : printf( "nTueday" ); break;
case 2 : printf( "nWednesay" ); break;
case 3 : printf( "nThursday" ); break;
case 4 : printf( "nFriday" ); break;
case 5 : printf( "nSaturday" ); break;
case 6 : printf( "nSunday" ); break;
}

return 0;
}

Leave a Reply

Leave a Reply

Your email address will not be published.