Day when the date is given


Day when the date is given

What if our calendar problem from aptitude gets converted to code and implemented? Want to check your accuracy during the practice sessions? Then let's check this out!!
For Example, Day:03 Month:11 Year:1999 is the date given for determining the day then the output processed is 'Wednesday'.
In this code, we are finding the day when the given date is given. It is programmed based on the concept of no: of odd days.
First of all, the data we need to feed is the no: of days in a month from Jan to Dec which is initialized in the form of an array called m[13].
Then the data is taken from stdin i.e. from the keyboard. Then the given year is divided into three parts.
Part I: The one with the 400 multiple.
part II: The one with 4 multiple.
Part III: The remaining part.
Based on the three different categories the no: of odd days are calculated.



Similarly, for months, the odd days are done based on the completed ones.
The resultant odd days are the remainders when cumulative ones are divided with 7.
Based on the resultant ones, from 0 to 6 the day in the week is finally predicted. Switch case is used.     



PROGRAM:
void main()
{
int day,mon,yr,sum=0,i,x,y,z,cum=0,a=0,b=0,c=0,ch;
int m[13]={31,28,31,30,31,30,31,31,30,31,30,31};//No: of days in a month
clrscr();
printf("Enter day\n");
scanf("%d",&day);
printf("\nEnter Month\n");
scanf("%d",&mon);
printf("\nEnter Year\n");
scanf("%d",&yr);
if(yr%100==0&&yr%400==0||yr%4==0)//Condition for a leap year
m[1]=29;//No: of days for a  leap year in Feb
//Calculating the no: of odd days starting with months
for(i=0;i<mon-1;i++)
sum=sum+m[i];
cum=cum+sum;
for(i=yr-1;i>0;i--)
{
if(i%400==0)//Calculating nearest 400 multiple in a given year
{
x=i;
break;//Classified the nearest 400 multiple
}
}
z=(yr-x)%100;/*Next turn: Separated the part
 which is not a multiple of 4*/
y=yr-x-z;//Traced  the multiple of 4
c=(z/4)*2+3*z/4;
b=(y/100)*5;/*No: of odd days present in the part of the year
 which is 400 multiple*/
cum=cum+a+b+c+day;//Total  No: of odd days are:
ch=cum%7;
printf("\nThe day is:");
switch(ch)       //Based on the resultant odd day the days in a week are:
{
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;
default:printf("Invalid");
}
getch();
}


Comments