# leap year function

This is a discussion on leap year function within the C Programming forums, part of the General Programming Boards category; alright, now I need to get the following function to tell me the number of leap years between two dates. ...

1. ## leap year function

alright, now I need to get the following function to tell me the number of leap years between two dates. It reads
Code:
```int leap(int year, int year_1)
{
for((year%400 ==0 || (year%100 != 0 && year%4 == 0));year<year_1;year++)
leapy+=leapy;
}```
As it is, it reads that every year is a leap. so for the distance from dates 1/1/1 to 1/1/100 it reads 99 leaps. Any suggestions?

2. Originally Posted by cameuth
alright, now I need to get the following function to tell me the number of leap years between two dates. It reads
Code:
`for((year%400 ==0 || (year%100 != 0 && year%4 == 0));year<year_1;year++)`
a for() statement is of the form...
Code:
`for( initialization ; test ; increment ) {}`
In other words you put a test condition in the initialization position where C does no test and acts on the expression only once.

3. Code:
```int leap(int year, int year_1)
{
for(year<year_1;(year%400 ==0 || (year%100 != 0 && year%4 == 0));year++)
leapy++;
}```
this reads out 0 instead of 100, how am i wrong now?

4. The most obvious way you are wrong is that you are just trying things at random, rather than actually trying to understand. (Before you deny that characterisation, I point out that the evidence for it is easily recognisable in your posts within this thread, so even a moderately experienced C programmer will KNOW that is exactly what you are doing. Denial will simply prove my point.)

The post by Tclausex is not a complete answer to your question. However, if you actually apply some effort to actually understanding that post then you will be one step closer to solving your problem.

5. yep, realized it after I posted. got it now though.

6. You need to ask yourself, why am I looping? what am I doing in each loop? what is controlling the number of times I loop?

You are trying to do too much with the for() expressions. There's a lot to say about for() loops, omitting expressions, and the comma operator, so I just gave you a short reply. I would recommend you just use a while() loop for this particular problem. It will force you to clean up your code and realize the logic needed.