Zeller's Congruence

I am getting some weird results like 2513 instead of 0-6 result. I've reviewed over and cant find anything wrong, could someone help?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>

using namespace std;

int main()
{
int year, month, day, h; 

cout << "Enter a year from the Gregorian Calendar: ";
cin >> year;
cout << "Enter a month from the Gregorian Calendar: ";
cin >> month;
cout << "Enter a day from the Gregorian Calendar: ";
cin >> day;

h = (day + (13 * (month + 1) / 5) + (year) + (year / 4) - (year / 100) + (year / 400) % 7);

cout << h << endl;

return 0;
}
Last edited on
Line 17 ends with ) % 7);, but I think you probably meant )) % 7;.
Thank you I am trying the solution right now.
Last edited on
Thank you very much the code works perfectly now and I did some refinements to it.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <string>

using namespace std;

int main()
{
    int year, month, day, h; 

    cout << "Enter a year from the Gregorian Calendar: ";
    cin >> year;
    cout << "Enter a month from the Gregorian Calendar: ";
    cin >> month;
    cout << "Enter a day from the Gregorian Calendar: ";
    cin >> day;

    h = (day + (13 * (month + 1) / 5) + (year) + (year / 4) - (year / 100) + (year / 400)) % 7;

    if (h == 1)
    {
        cout << "The day is Sunday.";
    }

    if (h == 2)
    {
        cout << "The day is Monday.";
    }

    if (h == 3)
    {
        cout << "The day is Tuesday.";
    }

    if (h == 4)
    {
        cout << "The day is Wednesday.";
    }

    if (h == 5)
    {
        cout << "The day is Thursday.";
    }
    
    if (h == 6)
    {
        cout << "The day is Friday.";
    }
    
    if (h == 7)
    {
        cout << "The day is Saturday.";
    }
    
    return 0;
}
Last edited on
Topic archived. No new replies allowed.