# Thread: Help with leap year program.

1. ## Help with leap year program.

I am making a martian leap year program(pointless program for class). This is the given :

1) All odd years are leap years.
2) All years divisible by 8, but not by 32 are leap years. (Thus, 8, 16 and 24 are leap years, but 32 is not. Similarly 40, 48 and 56 are leap years but 64 isn't.)

I made a double if statment but it is not working. When I just had part 2 it worked fine but when I added the odd years it is not working.

Code:
```#include <stdio.h>

int main() {

int year;

printf("Please enter a martian year\n");
scanf("%d", &year);
/* leap years occur if: years can be divided by 8 evenly and not by 32*/
if (year%8 == 0 && year%32 != 0){
if(year%2 == 1)
printf("\n%d is a leap year", year);
}
else
printf("\n%d is not a leap year", year);

return 0;

}```

2. Watch your indentation!

Code:
```if (year%8 == 0 && year%32 != 0){
if(year%2 == 1)
printf("\n%d is a leap year", year);
}
else
printf("\n%d is not a leap year", year);```

3. I did not think indention would make a difference when it runs. I tried it anyways and it did not work.

4. Watch your if statements. Your program is making the assumption that all odd numbers are divisible by 8 and not divisible by 32. Each of your conditions are independant of each other. By nesting your statements, you make the interior conditions dependant on the exterior.

5. Alright, I actually didn't read your post very thoroughly... :P But seriously, all odd years are leap years? Are we talking leap years as in an extra day in February? 'Cause I thought that was something that occured every fourth year, with the exception of some simple algorithm which I guess you've layed out above.

Anyhow, you need to rethink the logic in your program. What you're saying above is something like "if divisible by 8 AND not divisible by 32 AND odd --> it's a leap year".

6. Tretton, you don't look very smart when you reply to a post without reading it. The program gets "Martian leap years". Now please, read... then answer.

7. Yes, you are correct. My brain isn't working tonight. I rest my case.

8. Originally Posted by IxTanGxI
I did not think indention would make a difference when it runs. I tried it anyways and it did not work.
It doesn't make a difference.

However, when you post code, the indentation usually tells us what you intend to be happening. In your case:
Code:
```if (year%8 == 0 && year%32 != 0){
if(year%2 == 1)
printf("\n%d is a leap year", year);
}
else
printf("\n%d is not a leap year", year);```
Because of where you've put the closing brace } it makes the else bound to the if (year%8... and NOT the if (year%2 which is what your intentation appears to suggest. So, with correct indentation (but the same code), it would look like as per tretton's first post.

If you actually want to do what your indentation suggests, it should be instead:
Code:
```if (year%8 == 0 && year%32 != 0) {
if (year % 2 == 1)
printf("\n%d is a leap year", year);
else
printf("\n%d is not a leap year", year);
}```

9. Code:
`if ( year % 2 == 1 || (year % 8 == 0 && year % 32 != 0) )`
An odd year or a year evenly divisible by 8 and not evenly divisible by 32.
1) All odd years are leap years.
2) All years divisible by 8, but not by 32 are leap years.

Popular pages Recent additions