Wap in c to find dayof a week from a given date.
//source code:#include
#include
#include
void main()
{
int dd,mm,yy,i,a,z,a1,a2,b,b1,b2,d=0,sm=0;
clrscr();
printf("enter date(DD/MM/YYYY):");
scanf("%d/%d/%d",&dd,&mm,&yy);
yy=yy-1;
a=yy%400;
b=yy/400;
a1=a%100;
b1=a/100;
a2=a1/4;
b2=a1-a2;
yy=yy+1;
for(i=1;i
if(i==2)
{
if(yy%100==0?yy%400==0:yy%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;
}
if(yy%100==0?yy%400==0:yy%4==0)
z=1;
else
z=0;
if(mm>12 || dd>31 || ((mm==4||mm==6||mm==9||mm==11)&& dd>30)||(z==0 && mm==2 && dd>28)||(z==1 && mm==2 && dd>29))
{
printf("Invalid date,Try again as(DD/MM/YYYY)");
exit(1);
}
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("\nSUNDAY"); break;
case 1: printf("\nMONDAY"); break;
case 2: printf("\nTUESDAY"); break;
case 3: printf("\nWEDNESDAY"); break;
case 4: printf("\nTHURSDAY"); break;
case 5: printf("\nFRIDAY"); break;
case 6: printf("\nSATURDAY"); break;
}
getch();
}
output
There are two concepts involved in finding the day of week.
1.concept of odd days.
2.Gregorian calender logic.
Concept of odd days:
We are supposed to find the day of the week on a given date.
For this, we use the concept of 'odd days'.
In a given period, the number of days more than the complete weeks are called odd days
For example:
number of days=7,then number of odd days is 0,
number of days=10,then number of odd days is =10%7=3,
number of days=250,then number of odd days is =250%7=5.
For an ordinary year(which is not leap year),the total number of days is 365,thus the number of odd days is =365%7=1,
For a leap year,the total number of days is 366,thus the number of odd days is=366%7=2.
Counting of Odd Days:
- 1 ordinary year = 365 days = (52 weeks + 1 day.)
1 ordinary year has 1 odd day.
- 1 leap year = 366 days = (52 weeks + 2 days)
1 leap year has 2 odd days.
- 100 years = 76 ordinary years + 24 leap years= (76 x 1 + 24 x 2) odd days = 124 odd days.= (17 weeks + days)
5 odd days.
Number of odd days in 100 years = 5.
Number of odd days in 200 years = (5 x 2)3 odd days.
Number of odd days in 300 years = (5 x 3)1 odd day.
Number of odd days in 400 years = (5 x 4 + 1)0 odd day.
- Concept of gregorian calender:Every four hundred years the gregogian calender repeats.
No comments:
Post a Comment
leave a comment here