Checking for Interger in C for Leap Years

  • Thread starter Thread starter madmike159
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around implementing a program in C to determine the day of the week for a given date, with a focus on correctly identifying leap years. Participants explore various methods for checking if a year is an integer and how to handle leap year calculations in the context of programming.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • One participant outlines the rules for determining leap years based on divisibility by 4, 100, and 400.
  • Another suggests using the modulus operator to check for remainders when dividing by 4.
  • A participant notes that integer division yields different results compared to float division.
  • Typecasting is mentioned as a potential solution for checking if a year is an integer.
  • Concerns are raised about the reliability of comparing float and integer divisions due to potential precision issues.
  • Some participants express confidence in their methods, while others question the effectiveness of certain approaches.

Areas of Agreement / Disagreement

Participants express a variety of methods for checking leap years and integer values, with no consensus on a single best approach. Some methods are endorsed while others are critiqued, indicating ongoing debate.

Contextual Notes

The discussion includes various assumptions about data types and their behavior in C, as well as potential pitfalls in comparing float and integer results. Specific limitations of the proposed methods are not fully resolved.

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 ·
Replies
6
Views
13K
  • · Replies 4 ·
Replies
4
Views
26K
  • · Replies 7 ·
Replies
7
Views
7K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
18K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 58 ·
2
Replies
58
Views
5K
  • · Replies 105 ·
4
Replies
105
Views
11K