Calculating the Date of Easter – Solution

Easter occurs later this month, on the 16th. You can discover that date by using Google or by running the code solution for this month’s Exercise.

The true purpose of the Exercise is to show you how a formula listed in pseudocode, or just written down as a series of steps, can be translated into C. In this example, the Easter computus requires about a dozen variables and multiple steps to accomplish.

For my solutions, I wrote an easter() function, which takes a year integer as an argument, along with pointers to hold the resulting month and day values for Easter. The year integer becomes the Y variable, which is the starting point for the computus.

The important parts of the easter() function are properly configuring the division operations with the order of precedence in mind; using the floor() function, which is declared in the math.h header file; and using the (int) typecast to ensure that some calculations are made properly.

Here is my easter() function:

void easter(int year, int *month, int *day)
{
    int Y,a,c,e,h,k,L;
    double b,d,f,g,i,m;

    Y = year;
    a = Y%19;
    b = floor(Y/100);
    c = Y%100;
    d = floor(b/4);
    e = (int)b%4;
    f = floor((b+8)/25);
    g = floor((b-f+1)/3);
    h = (19*a+(int)b-(int)d-(int)g+15)%30;
    i = floor(c/4);
    k = c%4;
    L = (32+2*e+2*(int)i-h-k)%7;
    m = floor((a+11*h+22*L)/451);
    *month = (int)floor((h+L-7*m+114)/31);
    *day = (int)((h+L-7*(int)m+114)%31)+1;
}

Because a C language function cannot return two variables, the values for Easter’s month and day are assigned to pointers, month and day. After calling easter(), pointer int variables month and day hold the month (3 for March and 4 for April) and day upon which Easter falls for year year.

In the Exercise, I asked for two programs, one that uses a loop to spin through years values 2001 and 2021 and another to return the date of Easter for the current year.

Click here to view my easter() function in the looping solution. Here is the output:

In 2001, Easter is on April 15
In 2002, Easter is on March 31
In 2003, Easter is on April 20
In 2004, Easter is on April 11
In 2005, Easter is on March 27
In 2006, Easter is on April 16
In 2007, Easter is on April 8
In 2008, Easter is on March 23
In 2009, Easter is on April 12
In 2010, Easter is on April 4
In 2011, Easter is on April 24
In 2012, Easter is on April 8
In 2013, Easter is on March 31
In 2014, Easter is on April 20
In 2015, Easter is on April 5
In 2016, Easter is on March 27
In 2017, Easter is on April 16
In 2018, Easter is on April 1
In 2019, Easter is on April 21
In 2020, Easter is on April 12
In 2021, Easter is on April 4

Click here to view my easter() function for the current-year solution. Here is that sample run:

This year, Easter is April 16, 2017

You may have concocted a different translation of the computus, which is fine. It contains some redundancies, so combining a couple of steps is okay, providing that the solutions match the proper dates for Easter.

Leave a Reply