Thread: Help with Easter Day Calculator

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    2

    Help with Easter Day Calculator

    Hi,

    As part of a college assignment we have been tasked to design a program that will calculate the date of Easter day for a range of years from 1583 onwards.

    Basically the end user has to be able to enter any range of years (to a maximum of 100) and the program will tell them when easter day is for the given years.

    So far I have the program working for when an individual year is put in, but i don't know how enter a range of years and get the required results.

    Any help would be much appreciated, as I don't know what to research next to solve this problem.

    Thanks in advance

    Chris

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    58
    Ideally, you would create a loop over the command that prompts you for the year.
    Show us what you have and we will offer suggestions.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    OH NOES! I *wanted* to know the day of Easter in 1582!!

  4. #4
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    If you've got the program working for one year, it should be very easy for a range - just repeatedly call your date-finding function for your given year, increasing it by 1, outputting the results for each year done. Repeats - loops are helpful for that.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    2
    Thanks for all the replys, I have posted the code that I have so far. The assignment is a little vague, it simply says "Write a C program that allows the user to enter a range of years (maximum 100) anytime after 1582 and for each year displays the date Easter Sunday falls on".

    I think this means that they enter a range of years (for example 1970 - 2000) and it will give easter day for all the years in between.


    Again thanks for your time, it is much appreciated.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    
    {
    int year,day;                                                                                       //Declarations
    int k,a,b,c,q,p,m,d,n,e;                                                                        //Declaration of Variables
    
      printf("THIS PROGRAM WILL TELL YOU THE DATE OF EASTER DAY");                                  //Title
      printf("\n\nPlease insert the range of years for which you would like to know when Easter Day is:");   //Input Year(s)
      scanf("%d",&year);                                                                            //Read in the Year(s)
      
      k = year/100;                                                                                 //Calculates k
      a = year%19;                                                                                  //Calculates a
      b = year%4;                                                                                   //Calculates b
      c = year%7;                                                                                   //Calculates c
      q = (3*k+3)/4;                                                                                //Calculates q
      p = (8*k+13)/25;                                                                              //Calculates p
      m = (15+q-p)%30;                                                                              //Calculates m
      d = (19*a+m)%30;                                                                              //Calculates d
      n = (4+q)%7;                                                                                  //Calculates n
      e = (2*b+4*c+6*d+n)%7;                                                                        //Calculates e
      
    
    
      if (d+e<=9)
    {
                 day = 22+d+e;
                 printf("The day is the %dth",day);
                 printf("\n\nThe Month is March\n\n");
    }
      else if (d==29, e==6)
    { 
                 printf("The day is the 19th");
                 printf("\n\nThe Month is April\n\n");
                 
    }
    
     else if (d=28,e==6,a>10)
    {
                 printf("The day is the 18th");
                 printf("\n\nThe Month is April\n\n");
    }
    
     else 
     
    {
                 day = d+e-9;
                 printf("The day is the %dth",day);
                 printf("\n\nThe Month is April\n\n");
    }
          
      
      
      
      system("PAUSE");	
      return 0;
    }

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    58
    I am not exactly sure where you obtained your algorithm to calculate Easter, especially the way those if conditions are coded. I have never seen an if condition coded this way. Usually if those are 'or' conditions, you would code something like:

    Code:
    if (d==28 || e==6 || a>10)
    The d=28 is not an condition test, but an assignment. You want '==' for testing values.

    Here is an alternate Easter algorithm:

    Code:
    $ cat easterday.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main ()
    {
    
     int cen, leap, days, temp1, temp2, year;
     int easter;
     int tries = 0;
     const int maxtries = 100;
     const int minyear  = 1582;
    
     printf("THIS PROGRAM WILL TELL YOU THE DATE OF EASTER DAY");
    
     while (tries < maxtries) {
       printf("\n\nPlease insert the year for which you would like to know when Easter Day is or enter zero to quit:  ");
       scanf("%d", &year);
    
       if (year == 0) {
          break;
       }
    
       if (year <= minyear) {
          printf("\n\nPlease enter a year after %d\n", minyear);
          continue;
       }
    
       cen = year % 19;
       leap = year % 4;
       days = year % 7;
       temp1 = (19 * cen + 24) % 30;
       temp2 = (2 * leap + 4 * days + 6 * temp1 + 5) % 7;
    
       easter = 22 + temp1 + temp2;
    
       if (easter > 31) {
          printf("Easter is %d04%02d\n", year, easter - 31);
       }
       else {
          printf("Easter is %d03%02d\n", year, easter);
       }
    
       tries++;
     }
    
     exit (0);
    }
    
    $ gcc -Wall easterday.c
    $ a.out
    THIS PROGRAM WILL TELL YOU THE DATE OF EASTER DAY
    
    Please insert the year for which you would like to know when Easter Day is or enter zero to quit:  2006
    Easter is 20060416
    
    
    Please insert the year for which you would like to know when Easter Day is or enter zero to quit:  2007
    Easter is 20070408
    
    
    Please insert the year for which you would like to know when Easter Day is or enter zero to quit:  2008
    Easter is 20080323
    
    
    Please insert the year for which you would like to know when Easter Day is or enter zero to quit:  2009
    Easter is 20090412
    
    
    Please insert the year for which you would like to know when Easter Day is or enter zero to quit:  1581
    
    
    Please enter a year after 1582
    
    
    Please insert the year for which you would like to know when Easter Day is or enter zero to quit:  2010
    Easter is 20100404
    
    
    Please insert the year for which you would like to know when Easter Day is or enter zero to quit:  0
    $

  7. #7
    Registered User
    Join Date
    Mar 2009
    Location
    the middle of nowhere
    Posts
    11
    where did you find the easter day formula. can you post a link to the fornula?

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    58
    I got this formula out of a book called "C Programmers' Toolkit" by Jack Purdum written in 1989. It might be out of print now.

  9. #9
    Registered User
    Join Date
    Mar 2009
    Location
    the middle of nowhere
    Posts
    11
    Quote Originally Posted by samf View Post
    I got this formula out of a book called "C Programmers' Toolkit" by Jack Purdum written in 1989. It might be out of print now.
    thank you. i'd really like to find a turtorial link that explains a lot of date calculations such as the easter calculation. anyone?

  10. #10

  11. #11
    Registered User
    Join Date
    Mar 2009
    Location
    the middle of nowhere
    Posts
    11
    thank you. the second link was really helpful. Ive found a lot of code samples for things like calculating julain days or calculating the day of the week etc. but none of the examples ever explain in detail why they use the magic numbers or the unusual calculations.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Should I learn a FULL language first?
    By Raeliean in forum Game Programming
    Replies: 8
    Last Post: 07-16-2005, 06:59 PM
  2. Oh Happy Day: new calculator =)
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 08-20-2003, 03:15 PM
  3. Easter Day?
    By Unregistered in forum C Programming
    Replies: 0
    Last Post: 03-30-2002, 06:25 PM
  4. debug program
    By new_c in forum C Programming
    Replies: 3
    Last Post: 03-18-2002, 11:50 PM
  5. Simplified code
    By soonerfan in forum C Programming
    Replies: 2
    Last Post: 12-05-2001, 03:50 PM