# Little help with leap years

This is a discussion on Little help with leap years within the C Programming forums, part of the General Programming Boards category; Ok, a leap year is any year divisible by 4 with the exception of any year divisible by 100 but ...

1. ## Little help with leap years

Ok, a leap year is any year divisible by 4 with the exception of any year divisible by 100 but not 400. I'm writing a program that will let the user input a year and it will output whether or not that year is a leap year. I have most of the code already written and don't really need help with that. I am a bit confused on how to write the actual calculating part itself though. Do you think it would be easier to use a switch or if/else ?

2. uhmm, why dont you post your code so the C elites would know what advice to give best

3. Well, here's what I got so far. Don't think it'll help much, since I've been waiting to figure out what is the best way to calculate if it's a leap year or not.

Code:
```#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
// Display Instructions
printf("Leaps years can be defined as any year divisible by 4 (except any year divisible by 100 but not 400. Enter a year below to find out if it if a leap year.");

//Input: year
int year;
printf("Enter the year:");
scanf("%d", &year);

//Figure out whether the year is a leap year or not.

return 0;
}```

4. >Do you think it would be easier to use a switch or if/else ?
Well, an if, seeing as how it's as simple as:
Code:
```if ( <leap year expression> ) {
/* Leap year */
}
else {
/* Not leap year */
}```
The expression is straightforward, and a direct translation of how it's described.

5. I'm sorry, I'm still kinda new to all this. I think I'm just being retarded, but what would be an expression to use for leap year expression? I just can't think of how to write it into a formula.

6. I will give you a hint. You use the modulo operator.

7. >I'm sorry, I'm still kinda new to all this.
Which is why we're going out of our way not to give you the answer straight out. You learn more if you figure it out on your own. But here's a more formal description:

If year is divisible by 4 and year is not divisible by 100 or year is divisible by 400. See what you can come up with.

8. The modulous operator returns the reamainder of a devision problem
it is % - SHIFT 5
so you would say
if (any year%4==0) then it is a leap year.. it can be devided evenly by 4

9. Ok, I think I got it. I wasn't sure exactly how to write out like "if year is divisible by 4" ect., but I think I got it now. Here is what I came up with
Code:
```#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
// Display Instructions
printf("Leap years can be defined as any year divisible by 4 (except any year divisible by 100 but not 400. Enter a year below to find out if it if a leap year.");

//Input: year
int year;
printf("\n\nEnter the year:");
scanf("%d", &year);
fflush(stdin);

//Figure out whether the year is a leap year or not.
if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
printf("\n%d is a Leap Year.", year);
else
printf("\n%d is not a Leap Year.", year);

getchar ();
return 0;
}```
Thanks for the help Prelude, Kivork, and relyt_123. You guys helped me out a bunch. Atleast I now know a little bit more about programming.

10. > printf("Leap years can be defined ..
> int year;
In C, all declarations must come before statements.
Check you're not using a C++ compiler to compile C code.

> fflush(stdin);
This is very poor - input streams cannot be flushed
See the FAQ.

11. Also for anti-ambiguity reasons (just to reaffirm the order) perhaps you should add an extra set of paranthisis in your if statement.

Code:
```if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
// to
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))```

12. Pretty sure there's supposed to be a #include <stdio.h> in there somewhere.

13. Code:
` #include "stdafx.h"`