# Thread: Need Help with C Program that determines day of the week

1. ## Need Help with C Program that determines day of the week

Hi, I've been working on this program for quick a while and I can't seem to figure it out. I have sample Test Data, but my program isn't working for all of them.

The program is supposed to take as input the appointment date in the form (year, month, day) and then compute which weekeday the appointment falls on. Dates for appointments have to be between January 1, 1901 and December 31, 2299

Program has to:
1. Read and validate the appointment date and if invalid print an appropriate message and terminate
2. Determine if the appointment year is a leap year
3. Calculate the number of leap years from 1901 to the appointment date.
4. Calculate the number of days from Jan 1 1901 to the appointment date
5. Determine the weekday corresponding to the appointment date.

Code:
```#include<stdio.h>

int main()
{

int year=0;
int day=0;

int month=0;
int week_day=0;
int total_days,difference,leap_yrs;

printf("Please enter an appointment day in the form year month day (YYYY MM DD):\n");
scanf("%d %d %d", &year, &month, &day);

/* Checks to see if the input is invalid, and if so prints out a "Date out of range" message and terminates the program*/

if (year<1901 || year>2299)
{
printf("Date out of range\n");
return 0;
}

if(((year<2000 && year%400 ==0) || (year<2000 && (year%100 != 0 && year%4 == 0))))
{
difference = ((year-1)%100);
leap_yrs = ((year%100) / 4); /*no. of leap years between concerned year and reference year*/
}
else if ((((year>2000 && year<=2100) && year%400 ==0) || ((year>2000 && year<=2100) && (year%100 != 0 && year%4 == 0))))
{
difference = (((year-1)%100)+100);
leap_yrs = difference / 4; /*no. of leap years between concerned year and reference year*/
}

else if ((((year>2100 && year<=2200) && year%400 ==0) || ((year>2100 && year<=2200) && (year%100 != 0 && year%4 == 0))))
{
difference = (((year-1)%100)+200);
leap_yrs = difference / 4; /*no. of leap years between concerned year and reference year*/
}

else if ((((year>2200 && year<=2299) && year%400 ==0) || ((year>2200 && year<=2299) && (year%100 != 0 && year%4 == 0))))
{
difference = (((year-1)%100)+300);
leap_yrs = difference / 4; /*no. of leap years between concerned year and reference year*/
}

else if(year>2000 && year<=2100)
{
difference = (((year-1)%100)+100);
leap_yrs = difference / 4;
}
else if ( year>2100 && year<=2200)
{
difference = (((year-1)%100)+200);
leap_yrs = difference / 4;
}
else if (year>2200 && year<=2299)
{
difference = (((year-1)%100)+300);
leap_yrs = difference / 4;
}
else
difference = ((year-1)%100);
leap_yrs = ((year%100) / 4);

total_days = ((difference*365) + (leap_yrs)+(day)) ;
week_day = total_days % 7;

/*converts month number to month name*/
printf("",month);
switch(month)
{
case 1: printf("January");
break;
case 2: printf("February");
break;
case 3: printf("March");
break;
case 4: printf("April");
break;
case 5: printf("May");
break;
case 6: printf("June");
break;
case 7: printf("July");
break;
case 8: printf("August");
break;
case 9: printf("September");
break;
case 10: printf("October");
break;
case 11: printf("November");
break;
case 12: printf("December");
break;
default: printf("Date out of range");
break;
}

printf(" %d, %d", day, year);
printf(" is a %d", week_day);
switch (week_day)
{
case 1: printf("Tuesday");
break;
case 2: printf("Wednesday");
break;
case 3: printf("Thursday");
break;
case 4: printf("Friday");
break;
case 5: printf("Saturday");
break;
case 6: printf("Sunday");
break;
case 7: printf("Monday");
break;

}
printf("\n");

return 0;
}```

so when I input 1901 1 1
I get: January 1, 1901 is a Tuesday (which is correct)

but then if I input 2299 3 15
I get: March 15, 2299 is a Thursday (which is INCORRECT, its supposed to be a Wednesday)