Path: utzoo!attcan!uunet!abvax!iccgcc!browns From: browns@iccgcc.decnet.ab.com (Stan Brown, Oak Road Systems) Newsgroups: comp.lang.c Subject: Re: Leap Year Checker. Message-ID: <1115.26ff47b6@iccgcc.decnet.ab.com> Date: 25 Sep 90 17:04:06 GMT References: <9464@uhccux.uhcc.Hawaii.Edu> <24700010@sunc1> Lines: 34 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.