Thursday, January 27, 2011

C Program to find day when a date is given

#include<stdio.h>
main(){
int d,m,y,i;
int r=0,r1=0,r2=0,s=0;
input:
printf("Enter the DATE MONTH and YEAR");
scanf("%d %d %d",&d,&m,&y);
 if(m==2){
   if((d>28)&&(y%4!=0)){
   printf("Fool,Invalid date in February of non leap year.The maximum value is 28");
     goto input;
     }
     if((d>29)&&(y%4==0)){
     printf(" Fool,Invalid date in February of leap year.The maximum value is 29");
     goto input;
     }
     }
 else if((m==1)||(m==3)||(m==5)||(m==7)||(m==8)||(m==10)||(m==12)){
    if(d>31){
    printf("Fool,Invalid date in %d th month.The maximum value is 31",m);
    goto input;
    }
    }
 else if(((m==4)||(m==6)||(m==9)||(m==11))&&(d>30)){
      printf("Fool,Invalid date in the %d th month.The maximum value is 30",m);
      goto input;
      }
 else if((d>31)){
printf(" Enter valid date");
goto input;
}
if((d<1)||(m<1)){
printf(" Fool,Enter positive valid data ");
goto input;
}
 else  {

switch(m){
case 1:
case 10:
r1=d+2;
break;
case 2:
case 3:
case 11:
r1=d+5;
break;
case 4:
case 7:
r1=d+1;
break;
case 9:
case 12:
r1=d;
break;
case 5:
r1=d+3;
break;
case 6:
r1=d+6;
break;
case 8:
r1=d+4;
break;
default:
{
printf(" Don't U know that there is no month greater than 12.Enter valid month Once again enter the Date Month Year ");

goto input;
}
}
}
if(y<2003){
  for(i=2002;i>=y;i--){
    if((i==y)&&(i%4==0)){
      if(m>2) s=s+1;
      else if(m<=2) s=s+2;
       break;
       }

       else if(i%4==0){
       if(i%100==0){
  if(i%400==0)
  s=s+2;
  else s=s+1;
  }
       else s=s+2;
 }
       else s=s+1;
       }
       r2=7-s%7;
       }
else if(y>2003){
 for(i=2004;i<=y;i++){
  if((i==y)&&(i%4==0)){
    if(m>2) s=s+2;
    else if(m<=2) s=s+1;
    break;
    }
     else if(i%4==0){
      if(i%100==0){
      if(i%400==0)
      s=s+2;
      else s=s+1;
      }
      else s=s+2;
      }
     else s=s+1;
     }
     r2=s%7;
     }
else r2=0;
r=(r1+r2)%7;
printf(" ");
switch(r){
case 0:
printf("The Day is SUNDAY");
break;
case 1:
printf("The Day is MONDAY");
break;
case 2:
printf("The Day is TUESDAY");
break;
case 3:
printf("The Day is WEDNESDAY");
break;
case 4:
printf("The Day is THURSDAY");
break;
case 5:
printf("The Day is FRIDAY");
break;
case 6:
printf("The Day is SATURDAY");
break;
}
printf("Thanks for using my program  ");
}

2 comments:

  1. how it is working can u explain d logic

    ReplyDelete
  2. Exactly! even I didn't get the logic....
    Please explain

    ReplyDelete