Easter Calculations

I can't seem to find the Error by the calculations are off by 1(sometimes +1 other times -1) and the months are totally wrong.

#include <iostream>
using namespace std;

int main()
{
double golden_number;
int startingyear, endingyear;
int julianepact, gregorianepact;
int solareq, lunareq;
int century;
int PFM;
int weekday;
int month;
int westerndate;

cout << "Enter Starting Year: ";
cin >> startingyear;
cout << endl;
cout << "Enter Ending Year: ";
cin >> endingyear;
cout << endl;

for (int i = startingyear; startingyear <= endingyear; startingyear++)
{
golden_number = (startingyear % 19) + 1;
julianepact = int(11 * (golden_number - 1)) % 30;
if (julianepact == 0)
{
julianepact == 30;
}
century = (startingyear / 100) + 1;
if ((startingyear % 4 == 0 && startingyear % 100 != 0) || (startingyear % 400 == 0))
{
solareq = ((3 * century) / 4);
}
else
{
solareq = ((3 * century) / 4);
solareq++;
}
if (startingyear == 2500)
{
lunareq = (8 * century + 5) / 25;
lunareq += 8;
}
else
{
lunareq = (8 * century + 5) / 25;
}
gregorianepact = julianepact - solareq + lunareq + 8;
if (gregorianepact < 1)
{
gregorianepact += 30;
}
else if (gregorianepact > 30)
{
gregorianepact -= 30;
}
switch (gregorianepact)
{
case 1: PFM = 12;
break;
case 2: PFM = 11;
break;
case 3: PFM = 10;
break;
case 4: PFM = 9;
break;
case 5: PFM = 8;
break;
case 6: PFM = 7;
break;
case 7: PFM = 6;
break;
case 8: PFM = 5;
break;
case 9: PFM = 4;
break;
case 10: PFM = 3;
break;
case 11: PFM = 2;
break;
case 12: PFM = 1;
break;
case 13: PFM = 31;
break;
case 14: PFM = 30;
break;
case 15: PFM = 29;
break;
case 16: PFM = 28;
break;
case 17: PFM = 27;
break;
case 18: PFM = 26;
break;
case 19: PFM = 25;
break;
case 20: PFM = 24;
break;
case 21: PFM = 23;
break;
case 22: PFM = 22;
break;
case 23: PFM = 21;
break;
case 24: PFM = 18;
break;
case 25: PFM = 18;
break;
case 26: PFM = 17;
break;
case 27: PFM = 16;
break;
case 28: PFM = 15;
break;
case 29: PFM = 14;
break;
case 30: PFM = 13;
break;
default:
break;
}
switch (gregorianepact)
{
case 1: month = 2; // april
break;
case 2: month = 2; // april
break;
case 3: month = 2; // april
break;
case 4: month = 2; // april
break;
case 5: month = 2; // april
break;
case 6: month = 2; // april
break;
case 7: month = 2; // april
break;
case 8: month = 2; // april
break;
case 9: month = 2; // april
break;
case 10: month = 2; // april
break;
case 11: month = 2; // april
break;
case 12: month = 2; // april
break;
case 13: month = 1; // march
break;
case 14: month = 1; // march
break;
case 15: month = 1; // march
break;
case 16: month = 1; // march
break;
case 17: month = 1; // march
break;
case 18: month = 1; // march
break;
case 19: month = 1; // march
break;
case 20: month = 1; // march
break;
case 21: month = 1; // march
break;
case 22: month = 1; // march
break;
case 23: month = 1; // march
break;
case 24: month = 2; // april
break;
case 25: month = 2; // april
break;
case 26: month = 2; // april
break;
case 27: month = 2; // april
break;
case 28: month = 2; // april
break;
case 29: month = 2; // april
break;
case 30: month = 2; // april
break;
default:
break;
}
switch (weekday = (((PFM + int(2.6 * month - 0.2) - 2 * century + startingyear + startingyear / 4 + century / 4) % 7) + 7) % 7) //Function Returning a Number for the day
{
case 0: weekday = 0; // sunday
break;
case 1: weekday = 1; // monday
break;
case 2: weekday = 2; // tuesday
break;
case 3: weekday = 3; // wednesday
break;
case 4: weekday = 4; // thursday
break;
case 5: weekday = 5; // friday
break;
case 6: weekday = 6; // saturday
break;
default:
break;
}

switch (weekday)
{
case 0: westerndate = PFM + 7;
if (westerndate > 31 && month == 1)
{
cout << startingyear << " " << "April" << " " << westerndate - 31 << endl;
}
else
cout << startingyear << " " << "March" << " " << westerndate << endl;
break;
case 1: westerndate = PFM + 6;
if (westerndate > 31 && month == 1)
{
cout << startingyear << " " << "April" << " " << westerndate - 31 << endl;
}
else
cout << startingyear << " " << "March" << " " << westerndate << endl;
break;
case 2: westerndate = PFM + 5;
if (westerndate > 31 && month == 1)
{
cout << startingyear << " " << "April" << " " << westerndate - 31 << endl;
}
else
cout << startingyear << " " << "March" << " " << westerndate << endl;
break;
case 3: westerndate = PFM + 4;
if (westerndate > 31 && month == 1)
{
cout << startingyear << " " << "April" << " " << westerndate - 31 << endl;
}
else
cout << startingyear << " " << "March" << " " << westerndate << endl;
break;
case 4: westerndate = PFM + 3;
if (westerndate > 31 && month == 1)
{
cout << startingyear << " " << "April" << " " << westerndate - 31 << endl;
}
else
cout << startingyear << " " << "March" << " " << westerndate << endl;
break;
case 5: westerndate = PFM + 2;
if (westerndate > 31 && month == 1)
{
cout << startingyear << " " << "April" << " " << westerndate - 31 << endl;
}
else
cout << startingyear << " " << "March" << " " << westerndate << endl;
break;
case 6: westerndate = PFM + 1;
if (westerndate > 31 && month == 1)
{
cout << startingyear << " " << "April" << " " << westerndate - 31 << endl;
}
else
cout << startingyear << " " << "March" << " " << westerndate << endl;
break;
}
}
system("pause");
return 0;
}
To start out, you have a for loop of for (int i = startingyear; startingyear <= endingyear; startingyear++), which is wrong. It should be for (int i = startingyear; i <= endingyear; i++)
Plus, in the program, you start with golden_number = (startingyear % 19) + 1; which is also wrong. It should be golden_number = (i % 19); as you are calculating the starting year with a new number with each pass. So, in each case you have startingyear, you should substitute an i instead, so the calculations are on the corresponding year in question.

Here is a much simplified version of finding Easter.

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
#include<iostream>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::string;

int main()
{
	int year, date, month, startingyear, endingyear;
	string Months[]={"","January","February","March","April","May","June","July","August","September","October","November","December"};
	int a, b, c, d, e, f, g, h, i, j, k, m, p;
	do {
		cout << "Enter Starting Year [Enter '0' to end program] : ";
		cin >> startingyear;
		cout << endl;

		if(startingyear>0)
		{
			cout << "Enter Ending Year: ";
			cin >> endingyear;
			cout << endl;
			for(year = startingyear;year < endingyear+1; year++)
			{
				a=year%19;
				b=year/100;
				c=year%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;
				month=(h+k-(7*m)+114)/31;
				p=(h+k-(7*m)+114)%31;
				date=p+1;
				cout << "Easter Day in" << " " << year << " " << "is on "  << Months[month]  << " " << date << ", " << year;
				cout << endl;
			}
		}
	} while (startingyear > 0 && startingyear < 5001);

	return 0; 
}
Thank you so much, i will check it out..
Topic archived. No new replies allowed.