# Thread: Day of the Week Programme

1. ## Day of the Week Programme

Hi everybody,

Here I come again. I feel so stuck... I'm faced with a coding exercise, the solution to which, I'm unable to find even after trying to break my head onto the wall....

The problem is that this is more of a beginner's exercise and as such I'm allowed to only use if statements!

Following is the thing:-

Q. According to the Gregorian calendar, it was Monday on the date 01/01/01. If any year is input through the keyboard, write a program to find out what is the day on 1st of January of the year input.

And, below is some scribbling that I tried. But, my head seems to go blank after that...

Code:
```/* Determination of Day on 01-01 */

#include<stdio.h>

int main()
{
int year, Day;

printf("\nEnter a Year: ");
scanf("%d",&year);

Day = /* Doomsday Algorithm, maybe... or Zeller's Congruence... aaah... forget it...*/

}```

2. 1. Find the Algorithm
2. Write code that does the Algorithm.

Which part are you having a problem doing?

Tim S.

3. Originally Posted by Anish Kuriakose
The problem is that this is more of a beginner's exercise and as such I'm allowed to only use if statements!
You already broke the rule because you've called two library functions printf and scanf.

4. January 1, 2001 = Monday.
So in 2002 it would be?
add 365 days - which is like adding exactly 52 weeks + 1 day. 52 x 7 + 1 = 365.
So the weekday slips ahead one day from Monday to Tuesday.
Then for 2003 the weekday would slip ahead again to Wednesday.
Then for 2004 the weekday would slip ahead again to Thursday.
For 2005 ... but 2004 is a leap year, so slip ahead 2 days to Saturday.
So now you need to figure out what's a leap year.
Now you also have to consider what happens if the input year is before 2001.

This is how you solve things. Do several examples by hand.

5. Yeah. That's my problem...! I'm unable to be able to figure out the algorithm.... I'll try this by doing some examples by hand.I guess I'm just too impatient...

6. Originally Posted by Anish Kuriakose
I guess I'm just too impatient...
i'm so sorry

7. Challenge was accepted.

Armed with the following information:
* Definition of a leap year;
* Start of the Gregorian calendar (year)
* How many days are in a normal year

I set about writing a function to determine the day of January 1st for any year in the Gregorian calendar. I used no references apart from what I listed above and it took me 45 minutes to derive a formula, implement it and debug it (and this was probably slow, I admit!)

Looking at my derived formula, and if I rearrange the formula and simplify, I have derived Gauss' method. I'm not great at maths, I applied mostly logic (on paper! the maths is pretty simple) and arrived at something that works and is the same as an accepted and published method. Surely if I can do this you can at least Google a formula and simply convert it to C notation.

Edit: What stumped me the most was how to deal with the years 1600 and 1700 and pondering on how to deal with those dates and keep the rest of the leap years rules "aligned" took me the most time. At first I was shifting the input year back in time and then something clicked (shifting back in time was a mistake, but, well, this is how problem solving works... you try things.) This can (and probably should) be done using paper and pencil.