Algorithm and C code to find day of Year.
Following is algorithm to find day of year:-
1) Input Year, month, day.
2) Take last 2 digit of Year as last_digit
3) Divide last 2 digit by 4 as divlast
4) Enter code for year as yearcode.
5) Enter code for month as monthcode.
6) Sum day,last_digit,divlast,yearcode,monthcode as add
7) If year is leap year and month=1 or 2 then add%7 and minus 1 and save as result.
8) else result=add%7
9)if result=0-->sunday;=1--.monday;=2-->tuesday;=3-->wednesday;=4-->thrusday;=5-->friday;=6-->saturday.
CODE:-
#include <stdio.h>
int main()
{
int y,m,d,yearcode,last_digit,divlast,monthcode,add,result;
printf("Enter the year= ");
scanf("%d",&y);
printf("Enter the month= ");
scanf("%d",&m);
printf("Enter the day= ");
scanf("%d",&d);
if(y<=3000 && m<=12 && d<=32)
{
last_digit=y%100;
divlast=last_digit/4;
if(y<=1699 && y>=1600)
{
yearcode=6;
}
else if(y<=1799 && y>=1700)
{
yearcode=4;
}
else if(y<=1899 && y>=1800)
{
yearcode=2;
}
else if(y<=1999 && y>=1900)
{
yearcode=0;
}
else if(y<=2099 && y>=2000)
{
yearcode=6;
}
if(m==1)
{
monthcode=0;
}
else if(m==2)
{
monthcode=3;
}
else if(m==3)
{
monthcode=3;
}
else if(m==4)
{
monthcode=6;
}
else if(m==5)
{
monthcode=1;
}
else if(m==6)
{
monthcode=4;
}
else if(m==7)
{
monthcode=6;
}
else if(m==8)
{
monthcode=2;
}
else if(m==9)
{
monthcode=5;
}
else if(m==10)
{
monthcode=0;
}
else if(m==11)
{
monthcode=3;
}
else if(m==12)
{
monthcode=5;
}
add=d+yearcode+divlast+last_digit+monthcode;
if(y%4==0 && m==1 || m==2 )
{
result=add%7;
result=result-1;
}
else
{
result=add%7;
}
if(result==0)
{
printf("%d year %d month %d day is sunday",y,m,d);
}
else if(result==1)
{
printf("%d year %d month %d day is monday.",y,m,d);
}
else if(result==2)
{
printf("%d year %d month %d day is Tuesday.",y,m,d);
}
else if(result==3)
{
printf("%d year %d month %d day is Wednesday.",y,m,d);
}
else if(result==4)
{
printf("%d year %d month %d day is Thrusday.",y,m,d);
}
else if(result==5)
{
printf("%d year %d month %d day is Friday.",y,m,d);
}
else if(result==6)
{
printf("%d year %d month %d day is saturday.",y,m,d);
}
}
else
{
printf("Please Enter the correct input");
}
return 0;
}
Comments
Post a Comment