Checking for Interger in C for Leap Years

  • Thread starter madmike159
  • Start date
In summary, the rule for determining whether a year is a leap year is as follows: any year that can be divided by 4 is a leap year, unless the year can be divided by 100. The year 2000, because it can be divided by 400, was a leap year.
  • #1
madmike159
Gold Member
371
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
  • #2
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.
 
  • #3
2001/4=500 if you use integers, and 500.25 if you use floats.
 
  • #4
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.
 
  • #5
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.
 
  • #6
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 ?
 
  • #7
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).
 
  • #8
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.
 
  • #9
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.
 

Related to Checking for Interger in C for Leap Years

1. What is a leap year?

A leap year is a year that contains an extra day, February 29th, to keep the calendar year synchronized with the astronomical or seasonal year.

2. How do I check if a year is a leap year in C?

In C, we can use the modulo operator (%) to check if a year is divisible by 4 or not. If the year is divisible by 4, we then check if it is also divisible by 100. If it is, we check if it is also divisible by 400. If any of these conditions are true, then the year is a leap year.

3. Can I use a conditional statement to check for leap years in C?

Yes, you can use a conditional statement, such as an if-else statement, to check for leap years in C. This is a common method used in programming to check for conditions and execute different blocks of code accordingly.

4. Why do we need to check for leap years in C?

We need to check for leap years in C to ensure that our calendar is accurate and synchronized with the Earth's orbit around the sun. If we did not have leap years, our calendar would gradually fall out of sync and we would experience changes in seasons and weather patterns over time.

5. Are there any built-in functions in C to check for leap years?

No, there are no built-in functions in C specifically for checking leap years. However, some libraries or frameworks may have functions that can assist with this task. It is also possible to write your own function to check for leap years in C.

Similar threads

Replies
1
Views
1K
  • Programming and Computer Science
Replies
6
Views
13K
  • Introductory Physics Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
6K
  • Art, Music, History, and Linguistics
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
26K
  • General Discussion
Replies
11
Views
2K
  • General Math
Replies
1
Views
960
  • Sci-Fi Writing and World Building
Replies
31
Views
3K
Replies
2
Views
3K
Back
Top