[comp.lang.c] Leap Year Checker.

bush@uhccux.uhcc.Hawaii.Edu (Anthony Bush) (09/20/90)

Hi Networld.
 I am currently making a bulletin board program and want to put an
interesting intro. Stuff like, todays date is so and so.. and there
are only so and so days left for christmas shopping and on this 
date in 1945 so and so happened.. The trouble I am having is figuring
out the code in C++ in which it can figure out if the current year
is a leap year or not. Any help is appreciated.
						thank you.
bush@uhccux.uhcc.hawaii.edu
bush@uhccux.bitnet

'if I didnt love you, Id hate you..'

mccaugh@sunc1.cs.uiuc.edu (09/22/90)

 Ordinarily, a leap-year is a multiple of four, so thst -- given leap-year y --
 (y%4 == 0) ought to indicate if y designates a leap-year.

pfalstad@phoenix.Princeton.EDU (Paul John Falstad) (09/23/90)

In article <24700010@sunc1> mccaugh@sunc1.cs.uiuc.edu writes:
> Ordinarily, a leap-year is a multiple of four, so thst -- given leap-year y --
> (y%4 == 0) ought to indicate if y designates a leap-year.

Except for century years.  Only century years divisible by 400 are
leap years.  The correct formula is:

(!(y % 4) && (y % 100 || !(y % 400)))

Sorry to split hairs, but this IS usenet, after all.

                This is the silliest sketch I've ever been in!

cpcahil@virtech.uucp (Conor P. Cahill) (09/23/90)

In article <24700010@sunc1> mccaugh@sunc1.cs.uiuc.edu writes:
>
> Ordinarily, a leap-year is a multiple of four, so thst -- given leap-year y --
> (y%4 == 0) ought to indicate if y designates a leap-year.

Actually a leap year is a year that is a multiple of 4 but not a multiple
of 100 unless it is also a multiple of 400.

So, 1900 is not a leap year, 1904 is, 1996 is, 2000 is, 2100 is not, etc.



-- 
Conor P. Cahill            (703)430-9247        Virtual Technologies, Inc.,
uunet!virtech!cpcahil                           46030 Manekin Plaza, Suite 160
                                                Sterling, VA 22170 

browns@iccgcc.decnet.ab.com (Stan Brown, Oak Road Systems) (09/26/90)

In article <24700010@sunc1>, mccaugh@sunc1.cs.uiuc.edu writes:
> Ordinarily, a leap-year is a multiple of four, so that--given leap-year y--
> (y%4 == 0) ought to indicate if y designates a leap-year.

Bzzzzzt!  Nope, but thanks for playing.  Vanna has lovely gifts for you.

Every leap year is divisible by four, but not every year divisible by
four is a leap year.  Since the 18th century (I think 1752, but an
earlier century in R.C. countries), the algorithm has been:

IF it's divisible by 400 it's a leap year
ELSEIF it's divisible by 100 it's _not_ a leap year
ELSEIF it's divisible by 4 it's a leap year
ELSE it's not a leap year.

Out of every 400 years, 97 are leap and 303 are non-leap.  (1800, 1900,
2100, 2200, 23300, 2500, etc. are non-leap years.  In countries where
people listened to Pope Gregory, 1700 was also not a leap year.)  Since
divisibility by 4 is necessary (though not sufficient), it's normally
coded first:

    if ( y%4  ||  (y%100==0 && y%400) )      /* inner ( ) are redundant */
	printf("%d is not a leap year.\n", y);
or
    if ( y%4==0  &&  (y%100 || y%400==0) )   /* inner ( ) required */
        printf("%d is a leap year.\", y);

Gosh, you ask a simple question and you get a pageant!  Of course, if
your program deals only with years from 1901 to 2099 inclusive, then "y
is divisible by 4" and "y is a leap year" are equivalent statements.

Stan Brown, Oak Road Systems, Cleveland, Ohio, U.S.A.      (216) 371-0043
Disclaimer:   Your mileage may vary.  Close cover before striking.   Void
where taxed, regulated, licensed, or prohibited by law. I am not a crook.

johnb@srchtec.UUCP (John Baldwin) (09/28/90)

 (Stan Brown, Oak Road Systems) writes:

|In article <24700010@sunc1>, mccaugh@sunc1.cs.uiuc.edu writes:
|> Ordinarily, a leap-year is a multiple of four, so that--given leap-year y--
|> (y%4 == 0) ought to indicate if y designates a leap-year.

|Bzzzzzt!  Nope, but thanks for playing.  Vanna has lovely gifts for you.

|Every leap year is divisible by four, but not every year divisible by
|four is a leap year.  Since the 18th century (I think 1752, but an
|earlier century in R.C. countries), the algorithm has been...

BRRRAPPP.  Sorry, your time's up.
           What do we have for the programmers, Johnny?


The date was 1582.

-- 
John T. Baldwin                     | "Pereant qui ante nos nostra dixerunt!"
Search Technology, Inc.             | (A plague on those who said our good
johnb%srchtec.uucp@mathcs.emory.edu |  things before we did!)

drh@duke.cs.duke.edu (D. Richard Hipp) (09/28/90)

>|> Ordinarily, a leap-year is a multiple of four, so that--given leap-year y--
>|> (y%4 == 0) ought to indicate if y designates a leap-year.
>|Every leap year is divisible by four, but not every year divisible by
>|four is a leap year.  Since the 18th century (I think 1752, but an
>|earlier century in R.C. countries), the algorithm has been...
>The date was 1582.

Pope Gregory the something-th instituted the Gregorian calendar beginning
on October 5, 1582.  Countries loyal to Rome followed suit immediately.
Other nations began to gradually convert to the Gregorian system over the
next 4 centuries.  The British Empire converted on September 2, 1752.
Russia waited until the 20th century before "going Gregorian".

You can probably guess that the non-uniformity of calendars in Europe in
prior centuries created some small confusion for international travelers.

yedinak@motcid.UUCP (Mark A. Yedinak) (09/28/90)

johnb@srchtec.UUCP (John Baldwin) writes:


: (Stan Brown, Oak Road Systems) writes:

:|In article <24700010@sunc1>, mccaugh@sunc1.cs.uiuc.edu writes:
:|> Ordinarily, a leap-year is a multiple of four, so that--given leap-year y--
:|> (y%4 == 0) ought to indicate if y designates a leap-year.

:|Bzzzzzt!  Nope, but thanks for playing.  Vanna has lovely gifts for you.

:|Every leap year is divisible by four, but not every year divisible by
:|four is a leap year.  Since the 18th century (I think 1752, but an
:|earlier century in R.C. countries), the algorithm has been...

:BRRRAPPP.  Sorry, your time's up.
:           What do we have for the programmers, Johnny?


:The date was 1582.

Sorry John, our judges reviewed your answer and it is incorrect. The original
answer was correct. The missing days were removed from September, in the year
1752. The dates removed were September 3 through September 13, inclusive. And
here is Jay to tell us what parting gifts we have for John.

-- 
Mark A. Yedinak - uunet!motcid!yedinak 		*  "Don't take life too
Motorola - General Systems Sector		*   seriously, you will
3205 Wilke Road, Arlington Heights, IL 60004	*   never get out of it
708-632-2874  (I said it, not the big M)	*         ALIVE!"

cze2529@dcsc.dla.mil (Dave Gaulden) (09/28/90)

In article <24700010@sunc1> mccaugh@sunc1.cs.uiuc.edu writes:
>
> Ordinarily, a leap-year is a multiple of four, so thst -- given leap-year y --
> (y%4 == 0) ought to indicate if y designates a leap-year.

In article <9464@uhccux.uhcc.Hawaii.Edu> bush@uhccux.uhcc.Hawaii.Edu (Anthony Bush) writes:
>date in 1945 so and so happened.. The trouble I am having is figuring
>out the code in C++ in which it can figure out if the current year
>is a leap year or not. Any help is appreciated.

The following is a standard leapyear algorithm function if it is 
any help.

main()
{
 blah...blah....

 if (month > 2)
     days += leapyear(year);

}

int leapyear(int year)      /* standard leapyear algorithm */
{
   if (year % 4 == 0 && year % 100 != 0 !! year % 400 == 0)
       return 1;
   else
       return 0;
}

Uses the return value to add the day if true.
-- 

"Man who says, 'It cannot be done', should not interrupt man who is doing it."

 Dave Gaulden cze2529@dcsc.dla.mil

cze2529@dcsc.dla.mil (Dave Gaulden) (09/28/90)

>main()
>{
> blah...blah....
>
> if (month > 2)
>     days += leapyear(year);
>
>}

**** Sorry, there was a typo of one of the operators. ****
 
 int leapyear(int year)      /* standard leapyear algorithm */
 {
    if (year % 4 == 0 && year % 100 != 0 !! year % 400 == 0)
        return 1;                        ^^
    else                    /* should be || above */
        return 0;
 }
 
 Uses the return value to add the day if true.


-- 

"Man who says, 'It cannot be done', should not interrupt man who is doing it."

 Dave Gaulden cze2529@dcsc.dla.mil

henry@zoo.toronto.edu (Henry Spencer) (09/30/90)

In article <654522456@grad11.cs.duke.edu> drh@duke.cs.duke.edu (D. Richard Hipp) writes:
>Russia waited until the 20th century before "going Gregorian".
>
>You can probably guess that the non-uniformity of calendars in Europe in
>prior centuries created some small confusion for international travelers.

It's still a headache when dealing with even relatively recent Russian
history, *not* helped by the Soviets' tendency to quote pre-changeover
dates using the new calendar, as if the change had happened earlier.
For example, you will often find two different dates for the birth of
historical figures like Tsiolkovskii, depending on choice of calendar.
-- 
Imagine life with OS/360 the standard  | Henry Spencer at U of Toronto Zoology
operating system.  Now think about X.  |  henry@zoo.toronto.edu   utzoo!henry

adrian@mti.mti.com (Adrian McCarthy) (10/03/90)

In article <1990Sep30.001756.7403@zoo.toronto.edu> henry@zoo.toronto.edu (Henry Spencer) writes:
>In article <654522456@grad11.cs.duke.edu> drh@duke.cs.duke.edu (D. Richard Hipp) writes:
>It's still a headache when dealing with even relatively recent Russian
>history, *not* helped by the Soviets' tendency to quote pre-changeover
>dates using the new calendar, as if the change had happened earlier.
>For example, you will often find two different dates for the birth of
>historical figures like Tsiolkovskii, depending on choice of calendar.

Ditto George Washington...  (February 22 and 11(?))

Aid.  (adrian@gonzo.mti.com)

tlg@ukc.ac.uk (T.L.Goodwin) (10/03/90)

In article <654522456@grad11.cs.duke.edu> drh@duke.cs.duke.edu 
(D. Richard Hipp) writes:
>...
>Russia waited until the 20th century before "going Gregorian".
>...

Aha, but the Russian system (where the year is a leap year iff the
remainder on division by 7 is 2 or 6) is actually more accurate* than
the Gregorian system, as well as not having the confusion at the end of
a century.

*i.e. better compensates for the fact that 1 year != 365 days

Regards,

Tim.
--
Tim Goodwin
UKnet Backbone

gwc@root.co.uk (Geoff Clare) (10/04/90)

In <2188@ukc> tlg@ukc.ac.uk (T.L.Goodwin) writes:

>Aha, but the Russian system (where the year is a leap year iff the
>remainder on division by 7 is 2 or 6) is actually more accurate* than
>the Gregorian system, as well as not having the confusion at the end of
>a century.

>*i.e. better compensates for the fact that 1 year != 365 days

Wrong.

There are approx. 365.242191 mean solar days in the tropical year.

The Gregorian system gives 365 + 1/4 - 1/100 + 1/400 = 365.2425 days. 
The Russian system (as described by Tim, I don't know if he's right)
gives 365 + 2/7 ~= 365.285714 days.  Even the Julian system (365.25 days)
was more accurate than that!
-- 
Geoff Clare <gwc@root.co.uk>  (Dumb American mailers: ...!uunet!root.co.uk!gwc)
UniSoft Limited, Hayne Street, London EC1A 9HH, England.   Tel: +44-71-315-6600

brendan@otc.otca.oz (Brendan Jones) (10/05/90)

in article <2188@ukc>, tlg@ukc.ac.uk (T.L.Goodwin) says:
> Aha, but the Russian system (where the year is a leap year iff the
> remainder on division by 7 is 2 or 6) is actually more accurate* than
> the Gregorian system, as well as not having the confusion at the end of
> a century.
> *i.e. better compensates for the fact that 1 year != 365 days

GARBAGE!

The Gregorian system (with 97 leap years over its 400 year repeating cycle) 
makes the length of an average year 365 days 5 hours 49 minutes and 
12.0 seconds.

The length of a siderial year (orbit of Earth around the Sun referenced to
the stars) is 365 days 6 hours 9 minutes 9.55 seconds (increasing by 95
microseconds per year), hence the Gregorian system underestimates this by 
19 minutes 57.5 seconds per year.  I think this standard is called UT0.

The Russian system has 2 leap years every 7 years, or an average year length 
of 365 days 6 hours 51 minutes and 25.7 seconds.  This overestimates the
length of a siderial year by 42 minutes 16.15 seconds per year, much worse
than the Gregorian.

However, the sidereal year is not used any more for accurate scientfic
purposes, UTC is now the time standard.  I believe the Gregorian system 
approaches this year length very closely, hence the need for only about 1 
leap second per year compensation.  Unfortunately, I do not have the reference 
for a year length referred to UT1 or UTC.  Perhaps someone can help.

-- 
Brendan Jones  | ACSnet:  brendan@otc.otca.oz.au               | witty quote
R&D Contractor |   UUCP:  {uunet,mcvax}!otc.otca.oz.au!brendan | deleted for
Services R&D   |  Phone:  (02)2873128     Fax:  (02)2873299    | safety
|||| OTC ||    |  Snail:  GPO Box 7000 Sydney 2001, AUSTRALIA  | reasons