Thread: Zeller's congruence not working

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    35

    Zeller's congruence not working

    im trying to write a program where a user inputs a date and the day of the week is then printed to the screen. so 3/4/2009 would print friday

    im using Zeller's congruence (Zeller's congruence - Wikipedia, the free encyclopedia) but it doesnt seem to give the correct result. here's my maths.
    Code:
       q  = DD; // DD is day entered by the user
       m  = MM; // MM is the month entered by the user
       m  = ( (m+1) * 26 ) / 10;
       K  = YYYY % 100; // YYYY is the year entered by the user
       KK = K / 4;
       J  = YYYY / 100;
       JJ = J / 4;
    
       DayOfTheWeek = q + m + K + KK + JJ - (2*J);
    
       DayOfTheWeek = DayOfTheWeek % 7;
    then if statements to print out the days with 0 = sat, 1 = sun ... 6 = fri.

    it doesnt work for some days eg 1/1/1700 was a friday, but my program will say it is a wednesday. ive tried the other formulae on the wiki page, like changing the -2J to +5j, but doesnt change anything. get the same errors

    but it does work for many days, which is why i think the problem may just be something minor

    so i was wondering if some fresh eyes could spot the flaw that i cant pick up. thx

  2. #2
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126
    probably is not the reason... but
    wikipedia says something like:
    DayOfTheWeek = q + floor(m) + K + floor(KK) + floor(JJ) - (2*J);
    and you didn't talk about what type is m, KK and JJ, please make sure you have the right values
    Mac OS 10.6 Snow Leopard : Darwin

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    35
    Quote Originally Posted by nacho4d View Post
    probably is not the reason... but
    wikipedia says something like:
    DayOfTheWeek = q + floor(m) + K + floor(KK) + floor(JJ) - (2*J);
    and you didn't talk about what type is m, KK and JJ, please make sure you have the right values
    they are all integer values

    I thought C automatically floors integer values which is why I did all of the smaller calculations involving flooring first and added everything at the end.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Did you note the last examples in the Wiki article that shows January 2000 should be treated as the 13 month of 1999?

    Apparently this is true for January and February, and then on March 1, the Zeller figures demand that to be the *third* month of the current year. So there is no month 1 or month 2 of the current year.

    Integer values are truncated, which is what Zeller's equation needs, but watch out for the modulo of a negative number. Such a number must be changed according to the Wiki article.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 08:08 PM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM