# Project 1

Write a program to compute the day of the week for any date of the Gregorian calendar. Here is the formula:

W=(C+Y+L+M+D ) mod 7


Y is the last two digits of the actual year and D is the actual day. You need to obtain the value of C from the following rule for the years:

• If year is in the 1400s, 1800s, 2200s, C=2
• If year is in the 1500s, 1900s, 2300s, C=0
• If year is in the 1600s, 2000s, 2400s, C=5
• If year is in the 1700s, 2100s, 2500s, C=4 Months are numbered from 1 in the usual way, but (from January) M is 0, 3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5

The only tricky part of this algorithm is L, the number of leap days that have occurred since the beginning of the century of the given date. To obtain this:

1. Integer divide the last two digits of the year by 4 to obtain the number of “ordinary” leap years in the century up to that year, not counting the century year itself if applicable.
2. Obtain the remainder of the two digits and 4. If it is not a century year and the remainder is 0 the year is a leap year, otherwise it is not. If the year itself is a century year see Step 3.
3. If the century (1400, 1500, etc.) was evenly divisible by 400 then the century year is a leap year, otherwise it is not. Thus 2000 was a leap year but 1900 was not. So add 1 for centuries divisible by 400 and 0 otherwise.
4. If your date is January 1-February 29 of a leap year, subtract 1. Try to devise a method to obtain the last two digits on your own. Print the day of the week as a word (Monday, Tuesday, etc.). Remember that Sunday is the first day of the week and it will be counted as 0 in this algorithm.

Test your program first with your own birth date. Then test with the following dates:

• Today’s date
• December 25, 1642 (Note: this is Newton’s birth date in the Julian calendar, but use it as a Gregorian date)
• October 12, 1492
• January 20, 2000
• December 11, 2525

Try to write and test your own program before peeking at the sample solution.

Sample solution

program day_of_week
!*******************************************************************************
! This program computes the day of the week given a date in the
! Gregorian calendar.
!
! Author:    K. Holcomb
! Changelog: 2013-06-05 Initial version
!            2014-01-29 Bug fix to correct leap_year test for century
!            2014-01-29 Added a loop for user input
!            2015-01-29 Modification to use only conditionals (no arrays)
!*******************************************************************************
implicit none

integer                          ::  max_year, min_year
integer                          ::  day, month, year, century
integer                          ::  D, M, Y, C, L, W
logical                          ::  leap_year, century_leap_year

day=2
month=3
year=2016

D=day
century=100*(int(year)/100)
Y=year-century

century_leap_year = mod(century,400)==0

leap_year=.false.
if (Y>0) then
leap_year = mod(year,4)==0
else
if (century_leap_year) leap_year=.true.
endif

L = int(Y)/4

if (century_leap_year) L=L+1

if (leap_year .and. month<3) L=L-1

if (month==1 .or. month==10) then
M=0
elseif (month==2 .or. month==3 .or. month==11) then
M=3
elseif (month==4 .or. month==7) then
M=6
elseif (month==5) then
M=1
elseif (month==6) then
M=4
elseif (month==8) then
M=2
else
M=5
endif

if     ( century==1400 .or. century==1800 .or. century==2200 ) then
C=2
elseif ( century==1500 .or. century==1900 .or. century==2300 ) then
C=0
elseif ( century==1600 .or. century==2000 .or. century==2400 ) then
C=5
elseif ( century==1700 .or. century==2100 .or. century==2500 ) then
C=4
else
print *, "This algorithm doesn't cover the century requested"
endif

W=mod((C+Y+L+M+D),7)

!Example of select case
select case(W)
case(0)
print *, 'Sunday'
case(1)
print *, 'Monday'
case(2)
print *, 'Tuesday'
case(3)
print *, 'Wednesday'
case(4)
print *, 'Thursday'
case(5)
print *, 'Friday'
case(6)
print *, 'Saturday'
end select

end program



Previous
Next