Day of Week Program

Feb 7, 2015 at 12:29am
I need to write a program that tells the day of the week, month, day, year, and whether it is a leap year or not from a input of MM DD YYYY form. I'm new to programming, so I am not really sure where to start. Any advice would be appreciated. Thanks

Example:
Enter numeric values for month, day and year of a date> 10 31 1990
Wednesday, October 31, 1990 occurred in a non-leap year.
Feb 7, 2015 at 12:38am
To calculate a leap year:
If the year is evenly divisible by 4, go to step 2. ...
If the year is evenly divisible by 100, go to step 3. ...
If the year is evenly divisible by 400, go to step 4. ...
The year is a leap year (it has 366 days).
The year is not a leap year (it has 365 days).

This could be represented by a Boolean value:
 
bool bLeapYear;


You can go to the different steps by using a series of nested if statements:
1
2
3
4
5
6
7
8
9
10
11
12
//Pseudo-code
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
bLeapYear = true;
}
}
}

else {
bLeapYear = false;
}

You only need one else because if the first statement is false, the others are also false.
Now you could also represent the month from if-else if blocks. From the main() function, you can print each one out and if the year is a leap year, then you can print that out.
Last edited on Feb 7, 2015 at 12:40am
Feb 7, 2015 at 1:36am
What do you think the best way to find the day of the week is?
Feb 7, 2015 at 2:02am
That leap year algorithm is not correct.
1
2
3
4
if (year % 400 == 0) return true;
else if (year % 100 == 0) return false;
else if (year % 4 == 0) return true;
else return false;

There are more compact ways to structure this, especially if you don't like multiple return statements in a function, but I believe this is the best way to demonstrate the logic.

For finding the day of the week, maybe something on this page will help:
https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week#Other_purely_mathematical_algorithms
Topic archived. No new replies allowed.