4

I tried writing the code for finding the day of the week for a given date using Zeller's Congruence but I'm not getting the correct output. What's wrong with my code?

#include <stdio.h>
#include <math.h>
int main()
{
  int h,q,m,k,j,day,month,year;
  printf("Enter the date (dd/mm/yyyy)\n");
  scanf("%i/%i/%i",&day,&month,&year);
  if(month == 1)
  {
    month = 13;
    year--;
  }
  if (month == 2)
  {
    month = 14;
    year--;
  }
  q = day;
  m = month;
  k = year % 100;
  j = year / 100;
  h = q + floor(13/5*(m+1)) + k + floor(k/4) +  floor(j/4) + 5 * j;
  h = h % 7;
  switch(h)
  {
    case 0 : printf("Saturday.\n"); break;
    case 1 : printf("Sunday.\n"); break;
    case 2 : printf("Monday. \n"); break;
    case 3 : printf("Tuesday. \n"); break;
    case 4 : printf("Wednesday. \n"); break;
    case 5 : printf("Thurday. \n"); break;
    case 6 : printf("Friday. \n"); break;
  }
  return 0;
}
Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
Shail
  • 881
  • 9
  • 20
  • 37
  • 2
    Seems from the Wikipedia article that you shouldn't be decrementing the year in the version of the formula that you use. – Yaniv Feb 28 '13 at 04:48
  • @Yaniv I tried removing the decrements, still the output wasn't correct. – Shail Feb 28 '13 at 04:54

2 Answers2

3

Here's a working version:

#include <stdio.h>
#include <math.h>
int main()
{
  int h,q,m,k,j,day,month,year;
  printf("Enter the date (dd/mm/yyyy)\n");
  scanf("%i/%i/%i",&day,&month,&year);
  if(month == 1)
  {
    month = 13;
    year--;
  }
  if (month == 2)
  {
    month = 14;
    year--;
  }
  q = day;
  m = month;
  k = year % 100;
  j = year / 100;
  h = q + 13*(m+1)/5 + k + k/4 + j/4 + 5*j;
  h = h % 7;
  switch(h)
  {
    case 0 : printf("Saturday.\n"); break;
    case 1 : printf("Sunday.\n"); break;
    case 2 : printf("Monday. \n"); break;
    case 3 : printf("Tuesday. \n"); break;
    case 4 : printf("Wednesday. \n"); break;
    case 5 : printf("Thurday. \n"); break;
    case 6 : printf("Friday. \n"); break;
  }
  return 0;
}

Live demo.

The key is this in your h formula: 13/5*(m+1). This is using integer division, which calculates 13/5 first, so the result is equivalent to 2*(m+1). Swap the 5 and (m+1) around and the result will be correct.

By the way you do need the year decrements if Jan/Feb, as the wiki article explains.

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
congusbongus
  • 13,359
  • 7
  • 71
  • 99
0

Why do you include "h= year % 100" and "j= year / 100"?????

#include <stdio.h>
#include <math.h>
int main()
{
  int h,q,m,k,j,day,month,year;
  printf("Enter the date (dd/mm/yyyy)\n");
  scanf("%i/%i/%i",&day,&month,&year);
  if(month == 1)
  {
    month = 13;
    year--;
  }
  if (month == 2)
  {
    month = 14;
    year--;
  }
  q = day;
  m = month;
  k = year % 100;
  j = year / 100;
  h = q + 13*(m+1)/5 + k + k/4 + j/4 + 5*j;
  h = h % 7;
  switch(h)
  {
    case 0 : printf("Saturday.\n"); break;
    case 1 : printf("Sunday.\n"); break;
    case 2 : printf("Monday. \n"); break;
    case 3 : printf("Tuesday. \n"); break;
    case 4 : printf("Wednesday. \n"); break;
    case 5 : printf("Thurday. \n"); break;
    case 6 : printf("Friday. \n"); break;
  }
  return 0;
}
  • You include "k= year % 100" and "j= year / 100" because k is defined to be the last two digits of the year, and j is defined to be the first two. So for 2019, k is 19, and j is 20. – MiguelMunoz Oct 18 '19 at 23:11