Missing something in dayofweek program
I had to write a program that prompts the user for three integers representing the day, month, and year of a given date (e.g. 5 24 1994), and that displays the day of the week for the given date (e.g. 5/24/1994 is a Tuesday). Below is the pseudo-code. I wrote the program but I seem to be missing something. When I execute the program, and I enter say, "5 12 1981" , the result shows "3 12 1981" subtracting two from the month. Why am I getting the adjusted month in my results? Can someone tell me what I'm missing?
a) First, adjust the month and year:
move the month 2 places down; that is; if the month entered is not January or February, subtract 2 from it to get the adjusted month, and make the adjusted year the same as the given year;
if the month is January or February, the adjusted month is month 11 or 12 of the previous year, which is the adjusted year.
b) Second, compute a month correction M, which is (26 times the adjusted month) minus 2, divided by 10 (integer division).
c) Third, compute the century C (actually century minus 1) and year Y within the century for the adjusted year (so, for example, if the adjusted year is 1993, C will be 19 and Y will be 93).
d) Fourth, compute a year correction Z which is Y + (Y divided by 4) + (C divided by 4) + (5 times C).
Again, all division are integer divisions.
e) Finally, compute a code D for the day of the week for the given date. D is the remainder after division by 7 of the sum of the day, month correction M and year correction Z. Now, if D is 0, 1, 2, …6 then, the day of the week is Sunday, Monday, Tuesday, …, Saturday respectively.
Here is my code:
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int day,month,year;
int M,C,Y,Z,D;
char more[2];
do
{
printf ("Enter the date: (mm dd yyyy)");
scanf ("%d %d %d",&month,&day,&year);
if (month > 2)
month -= 2;
else
{
month = 10 + month;
year --;
}
M = ((26*month)-2)/10;
C = year/100;
Y = year % 100;
Z = (Y)+(Y/4)+(C/4)+(Y*C);
D = (day+M+Z)%7;
if (D=0)
{
printf("%d/%d/%d is a Sunday\n",month,day,year);
}
else if (D=1)
{
printf("%d/%d/%d is a Monday\n",month,day,year);
}
else if (D=2)
{
printf("%d/%d/%d is a Tuesday\n",month,day,year);
}
else if (D=3)
{
printf("%d/%d/%d is a Wednesday\n",month,day,year);
}
else if (D=4)
{
printf("%d/%d/%d is a Thursday\n",month,day,year);
}
else if (D=5)
{
printf("%d/%d/%d is a Friday\n",month,day,year);
}
else if (D=6)
{
printf("%d/%d/%d is a Saturday\n",month,day,year);
}
printf ("Continue? (y/n):");
scanf ("%s",&more);
}
while (more [0] != 'n');
return 0;
}