I want to create a program that will calculate the day easter will be on. The user will type in a year and then the program should calculate the day easter will be on. I'm having trouble understanding the if/else and how i can use that with the code

// EasterProject
#include<iostream>
    using namespace std;
int main()
{ 
    int intyear, int intEasterDate, int intEasterMonth;
    int a, b, c, d, e, f, g, h, i, j, k, m, p;

cout<< "What's the year? \n";
    cin>> intyear;

Cout<<"Easter Month is \n";

if (intyear<0001||intyear>5000)
    {cout<<"This calculator cannot serve you\n"
    return -1;}
a=intyear%19;
b=intyear/100;
c=intyear%100;
d=b/4;
e=b%4;
f=(b+8)/25;
g=(b-f+1)/3;
h=((19*a)+b-d-g+15)%30;
i=c/4;
j=c%4;
k=(32+(2*e)+(2*i)-h-j)%7
m=(a+(11*h)+(22*k))/451
intEasterMonth=(h+k-(7*m)+114)/31
p=(h+k-(7*m)+114)%31
intEasterDate=p+1;


return 0;
}

Recommended Answers

All 7 Replies

Did you mean something like this?

if (year not ok)
{
    cout wrong year message
    return -1;
}
else
{
    do easter calculation
    return 0;
}

I solved your project syntax, but alghoritm not correct

// EasterProject
#include<iostream>
    using namespace std;
int main()
{ 
    int intyear, intEasterDate, intEasterMonth;
    int a, b, c, d, e, f, g, h, i, j, k, m, p;
do {
cout<< "What's the year? \n";
    cin>> intyear;

cout<<"Easter Month is \n";

if (intyear<0001||intyear>5000)
    { cout<<"This calculator cannot serve you\n";
    return -1; }
a=intyear%19;
b=intyear/100;
c=intyear%100;
d=b/4;
e=b%4;
f=(b+8)/25;
g=(b-f+1)/3;
h=((19*a)+b-d-g+15)%30;
i=c/4;
j=c%4;
k=(32+(2*e)+(2*i)-h-j)%7;
m=(a+(11*h)+(22*k))/451;
intEasterMonth=(h+k-(7*m)+114)/31;
p=(h+k-(7*m)+114)%31;
intEasterDate=p+1;
cout << intEasterMonth << "." << intEasterDate << "\n\n";
} while (intyear>=0001&&intyear<=5000);

return 0;
}

ddanbe, I compared to a built-in PHP function "easter_date()" and adjust alghoritm to C++

#include <iostream>
using namespace std;

void EasterDate(int Year)
    {
        // Gauss Calculation
        ////////////////////

        int Month = 3;

        // Determine the Golden number:
        int G = Year % 19 + 1;

        // Determine the century number:
        int C = Year / 100 + 1;

        // Correct for the years who are not leap years:
        int X = ( 3 * C ) / 4 - 12;

        // Mooncorrection:
        int Y = ( 8 * C + 5 ) / 25 - 5;

        // Find sunday:
        int Z = ( 5 * Year ) / 4 - X - 10;

        // Determine epact(age of moon on 1 januari of that year(follows a cycle of 19 years):
        int E = ( 11 * G + 20 + Y - X ) % 30;
        if (E == 24) {E++;}
        if ((E == 25) && (G > 11)) {E++;}

        // Get the full moon:
        int N = 44 - E;
        if (N < 21) {N = N + 30;}

        // Up to sunday:
        int P = ( N + 7 ) - ( ( Z + N ) % 7 );

        // Easterdate: 
        if ( P > 31 )
        {
            P = P - 31;
            Month = 4;
        }
        cout << Year << "." << Month << "." << P;
    }

int main ()
{
    int year;

    for(int i=1970; i<2038; i++){
        EasterDate(i);
        cout << endl;
        }
    return 0;
}

also incorrect on 1970-1989 years.

(function easter_date() will generate a warning if the year is outside of the range for Unix timestamps (i.e. before 1970 or after 2037).)

This generate width PHP:
1970-03-29
1971-04-10
1972-04-01
1973-04-21
1974-04-13
1975-03-30
1976-04-17
1977-04-09
1978-03-26
1979-04-14
1980-04-05
1981-04-19
1982-04-11
1983-04-03
1984-04-22
1985-04-07
1986-03-30
1987-04-19
1988-04-03
1989-03-26
1990-04-15
1991-03-31
1992-04-19
1993-04-11
1994-04-03
1995-04-16
1996-04-07
1997-03-30
1998-04-12
1999-04-04
2000-04-23
2001-04-15
2002-03-31
2003-04-20
2004-04-11
2005-03-27
2006-04-16
2007-04-08
2008-03-23
2009-04-12
2010-04-04
2011-04-24
2012-04-08
2013-03-31
2014-04-20
2015-04-05
2016-03-27
2017-04-16
2018-04-01
2019-04-21
2020-04-12
2021-04-04
2022-04-17
2023-04-09
2024-03-31
2025-04-20
2026-04-05
2027-03-28
2028-04-16
2029-04-01
2030-04-21
2031-04-13
2032-03-28
2033-04-17
2034-04-09
2035-03-25
2036-04-13
2037-04-05

Member Avatar for 1stDAN

Hi,

you are about to implement the famous Meeus/Jones/Butcher algorithm in c++. Among others, this algorithm, also called "The anonymous Gregorian algorithm", can be found on Click Here.

Unfortunately, you didn't correctly translate the floor function, for example b = floor (Y / 100) of MJB algorithm. floor(x) returns the largest integer not greater than x, e.g. floor (9.999) is 9.0.

c++ has also floor() function, see math.h. So simply put each of your statements where a division is applied into a floor() call, for example:

b=floor(intyear/100);
...
intEasterMonth=floor((h+k-(7*m)+114)/31));

I checked again, but PHP generated dates are not always on Sunday - so wrong!
This works in C++ and it seems that the correct:

#include <sstream>
#include <iostream>


using namespace std;

string ZeroPadNumber(int num){
    stringstream ss;
    // the number is converted to string with the help of stringstream
    ss << num; 
    string ret;
    ss >> ret;
    // Append zero chars
    int str_length = ret.length();
    for (int i = 0; i < 2 - str_length; i++)
        ret = "0" + ret;
    return ret;
    }

void EasterDate(int X){                             // X = year to compute
    int K, M, S, A, D, R, OG, SZ, OE;

    K  = X / 100;                                   // Secular number
    M  = 15 + (3 * K + 3) / 4 - (8 * K + 13) / 25;  // Secular Moon shift
    S  = 2 - (3 * K + 3) / 4;                       // Secular sun shift
    A  = X % 19;                                    // Moon parameter
    D  = (19 * A + M) % 30;                         // Seed for 1st full Moon in spring
    R  = D / 29 + (D / 28 - D / 29) * (A / 11);     // Calendarian correction quantity
    OG = 21 + D - R;                                // Easter limit
    SZ = 7 - (X + X / 4 + S) % 7;                   // 1st sunday in March
    OE = 7 - (OG - SZ) % 7;                         // Distance Easter sunday from Easter limit in days

    //Easter = DateSerial(X, 3, OG + OE);           // Result: Easter sunday as number of days in March
    cout << X << "-" << ZeroPadNumber(((OG + OE)>31)?4:3) << "-" << ZeroPadNumber((((OG + OE)%31)==0)?31:((OG + OE)%31));
    }

int main ()
{
    int year;
    do {
        cout << "Put the year: ";
        cin >> year;
        EasterDate(year);
        cout << endl << endl;
        }
    while (year>0);
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.