Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Zeller's congruence is an algorithm to calculate the day of the week. The formula is:

h=(q+⌊26(m+1)10⌋+k+⌊k4⌋+⌊j4⌋+5j)%7
where:

⌊…⌋ indicates floor division
h is the day of the week (0 - Saturday, 1 - Sunday, 2 - Monday, 3 - Tuesday, 4 - Wednesday, 5 - Thursday, 6 - Friday)
q is the day of the month
m is the month (3 - March, 4 - April, ... 12 - December). January and February are counted as months 13 and 14 of the previous year, e.g. for January 25 2013, you would use the 25th day of the 13th month in 2012.
j is the century, e.g. 20 for 2013
k is the year of the century, e.g. 13 for 2013.
Write a program using functions that prompts the user to enter a year (e.g. 2008), month (e.g. 1-12), and day of the month (e.g. 1-31), and then displays the name of the day of the week. For example, for year 2013, month 1, and day of month 25, the day of the week is Friday; the user inputs 2013, 1, and 25 in response to your prompts and then you calculate h which will be 6, and then output Friday. Run the program for five different dates. Testing must include different months including January and February, different centuries, and different years.


Now for this question i used 2 modules: 1 being the function module and 1 being my main program.

Function module:
VB
h =(q+(26*m+1))/((10)+k+(k/4)+(j/4)+(5*j))%7
   if h==1:
       day="Sunday"
   elif h==2:
       day="Monday"
   elif h==3:
       day="Tuesday"
   elif h==4:
       day="Wednesday"
   elif h==5:
       day="Thursday"
   elif h==6:
       day="Friday"
   else:
       day="Saturday"
   return day



Main program module:

VB
import q5_function

q = int (input("enter day of the month"))
m = int (input ("enter the month"))
j = int (input("enter the century"))
k = int (input("enter the year of the century"))
h=q5_function.func_day(q, m, j, k)
print ("the day is {0}".format(h))



Now if i try and run this program it keeps printing "saturday" and nothing els.
How can i get the formula to work?
Posted
Comments
PIEBALDconsult 8-Feb-15 22:23pm    
http://www.codeproject.com/Forums/326859/Algorithms.aspx
PIEBALDconsult 8-Feb-15 22:31pm    
That function doesn't seem to match what I see on Wikipedia
http://en.wikipedia.org/wiki/Zeller%27s_congruence
Richard MacCutchan 9-Feb-15 5:17am    
Part of the problem is that the result of your calculation will be a floating point number (python prefers floating point), which will not match any of the integer values, so it will always report Saturday. I am looking into a solution, but cannot get either your or Shridhar Gowda's formula to work.

Hi bro, actually formula you used was wrong. Use this formula (refer wikipedia).

C#
int h1 = (q + ((13 * (m + 1)) / 5) + k + (k / 4) + (j / 4) + (5 * j)) % 7;


I implemented the same in c#.NET, It works perfects...!
 
Share this answer
 
Comments
Richard MacCutchan 9-Feb-15 4:52am    
Tried that and the answer is not correct.
Shridhar Gowda 9-Feb-15 5:07am    
This is the code I written in c#. It works exactly... Convert it into Your language.

class Program
{
static void Main(string[] args)
{
Console.WriteLine("enter day of the month");
int q = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("enter the month");
int m = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("enter the century");
int j = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("enter the year of the century");
int k = Convert.ToInt32(Console.ReadLine());


string h = calculateDay(q, m, j, k);
Console.WriteLine("the day is {0}", h);
Console.ReadKey();
}

public static string calculateDay(int q, int m, int j, int k)
{
int h = (q + (26 * m + 1)) / ((10) + k + (k / 4) + (j / 4) + (5 * j)) % 7;
int h1 = (q + ((13 * (m + 1)) / 5) + k + (k / 4) + (j / 4) + (5 * j)) % 7;
Console.WriteLine("h: " + h + " h1: " + h1);
string day = string.Empty;
switch (h1)
{
case 1:
day = "Sunday";
break;
case 2:
day = "Monday";
break;
case 3:
day = "Tuesday";
break;
case 4:
day = "Wednesday";
break;
case 5:
day = "Thursday";
break;
case 6:
day = "Friday";
break;
case 7:
day = "Saturday";
break;
default:
day = "h1: " + h1 + " wrong calculation";
break;
}
return day;
}
}
Richard MacCutchan 9-Feb-15 5:14am    
That tells me that today is Friday, rather than Monday.
Shridhar Gowda 9-Feb-15 5:38am    
This is output for today's date:
Enter day of the month
9
enter the month
14
enter the century
20
enter the year of the century
14
The day is Monday

I am sure you might have missed input parameters
Richard MacCutchan 9-Feb-15 5:46am    
Where does month=14 and year=14 for today (9/2/2015) come from?
Here is a function that seems to work (I have not tested against every date in the last hundred years):
Python
def dayname(date, month, year):   # Day, Month, Year in full
    if (month < 3):
        # January and February are months 13 and 14 of the previous year
        month += 12
        year -= 1;
        
    # separate out the century and year parts
    j = int(year / 100)
    k = int(year % 100)
    
    # calculate the day of the week
    dow = int((date + ((13 * (month + 1)) / 5) + k + int(k / 4) + int(j / 4) + (5 * j)) % 7)

    if dow == 1:
        day = "Sunday"
    elif dow == 2:
        day = "Monday"
    elif dow == 3:
        day = "Tuesday"
    elif dow == 4:
        day = "Wednesday"
    elif dow == 5:
        day = "Thursday"
    elif dow == 6:
        day = "Friday"
    else:
        day = "Saturday"

    return day

Call the function like:
Python
import dayname
print('Today is', dayname.dayname(9,2,2015))
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900