Friday 23 March 2012

Program in c to find day corresponding to given date.

// Program in c to find day corresponding to given date.
#include<stdio.h>
#include<conio.h>
void main()
{
 int dd,mm,yy,i,a,a1,a2,b,b1,b2,d=0,sm=0;
 clrscr();
 printf("enter date(DD/MM/YYYY):");
 scanf("%d%d%d",&dd,&mm,&yy);
 if(mm>12 || dd>31)
   printf("you entered wrong date");
 else
   {
    yy=yy-1;
    a=yy%400;
    b=yy/400;
    a1=a%100;
    b1=a/100;
    a2=a1/4;
    b2=a1-a2;
    for(i=1;i<mm;i++)
     {
      if(i==2)
       {
                if((yy+1)%4==0)
                 d=d+29;
                else
                 d=d+28;
       }
      else if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
       d=d+31;
      else
       d=d+30;
     }
    sm= b*0+b1*5+a2*2+b2*1+d+dd;
    sm=sm%7;
    printf("according to entered date day is ");
    switch(sm)
     {
      case 0: printf("sunday"); break;
      case 1: printf("monday"); break;
      case 2: printf("tuesday"); break;
      case 3: printf("wednesday"); break;
      case 4: printf("thursday"); break;
      case 5: printf("friday"); break;
      case 6: printf("saturday"); break;
     }
   }
 getch();
}
output:

Enter date (DD/MM/YYYY)-22 2 1992

According to entered date Day=   Saturday

8 comments:

  1. man i would like to know the algorithm

    ReplyDelete
  2. This Program is not giving correct Output.

    You have to make a little change in the checking of Leap year inside the if condition. "if((yy+1)%4==0)"This has to be replaced with the correct logic
    Declare Year = yy; after having the input and
    "if((year%400 == 0)||((year%100 != 0)&&(year%4 == 0)))"......

    Happy Programing...:-]

    ReplyDelete
  3. what is logic for this program please.............

    ReplyDelete
  4. thanku there is need to change the leap year condition

    ReplyDelete
  5. https://www.youtube.com/watch?v=8vMGcfY7MpI

    ReplyDelete
  6. sm= b*0+b1*5+a2*2+b2*1+d+dd;
    why we use this formula

    ReplyDelete