Checking for Interger in C for Leap Years

  • Thread starter Thread starter madmike159
  • Start date Start date
AI Thread Summary
The discussion centers on creating a C program to determine the day of the week for a given date, with a focus on correctly accounting for leap years. The leap year rules are clarified: years divisible by 4 are leap years unless they are divisible by 100, with the exception that years divisible by 400 are also leap years. Participants discuss methods to check if a year is a leap year, emphasizing the use of the modulus operator (%) to determine remainders. Suggestions include checking if the division of the year by 4 results in a remainder, which is a straightforward approach. There is also mention of typecasting and the potential pitfalls of comparing float and integer divisions, with a consensus that the modulus operator is the most reliable method. The conversation concludes with an acknowledgment of various coding approaches and the intention to test different methods for optimal results.
madmike159
Gold Member
Messages
369
Reaction score
0
I am making a program in C to tell the user what day of the week at date will be (eg. 1st Jan 2000 is a Saturday).
I need to account for leap years, the rule for them is below.

All years divisible by 4 are leap years unless the year can be divided by 100. There is, however, an exception to this 100 year rule exception. Any year that can be divided by 400 is a leap year. So while the years 1700, 1800 and 1900 were not leap years because they are divisible by 100, the year 2000, because it is divisible by 400, was a leap year.

I can divide the year 2001 by for and get 500.24, but how can I know that its not a interger?
 
Technology news on Phys.org
The simplest is to check whether division by 4 produces a remainder. The operator you want is "%":

2000 % 4 == 0
2001 % 4 == 1
2002 % 4 == 2
2003 % 4 == 3
2004 % 4 == 0
etc.
 
2001/4=500 if you use integers, and 500.25 if you use floats.
 
CompuChip's reply about the "modulus" operator should work perfectly.

If you are unfamiliar with typecasting [ like "(int)" ] you might want to look into that, although your current problem seems to be solved.
 
I found something on type casting (I had a lecture on it last week...)
if (year/4 == (int)year);

I forgot the remainder operator even existed.
Thanks for the help guys.
 
madmike159 said:
if (year/4 == (int)year);

No idea what was your plan, but that would not check if the year is leap.

8/4 = 8 ?
 
You could check whether the float division gives the same answer as the integer divison. I.e., something like

(float)year/4.0 == (float)((int)year/4)

However, this is ugly (the modulo operator makes it immediately clear what is meant) and risky (the float division of 2001/4 may give 500.2499999 while the integer division cast to float gives 500.250000..., and the comparison fails).
 
No that code worked, I tested it out. It checks if year would go into a int, if its true the if statement will run.
 
Sure it works.
But you can shorten it to
if(year == 0);
 
  • #10
There seem to be loads of ways of doing this. I'm just going to test a few and see which is best. Thanks for the help guys.
 

Similar threads

Replies
6
Views
13K
Replies
4
Views
26K
Replies
7
Views
7K
Replies
2
Views
2K
Replies
2
Views
4K
Replies
2
Views
18K
Back
Top