-5

I really don't understand the logic of performing:-

if(num%400==0)
stmts;
else if(num%100==0)
stmts;

for testing a leap year. Isn't it enough for just finding modulus of 100?

Thanks in advance!!

2 Answers2

1

The logic behind whether a year is leap or not:

  1. If the year is evenly divisible by 4, go to step 2. Otherwise, go to step 5.
  2. If the year is evenly divisible by 100, go to step 3. Otherwise, go to step 4.
  3. If the year is evenly divisible by 400, go to step 4. Otherwise, go to step 5.
  4. The year is a leap year (it has 366 days).
  5. The year is not a leap year (it has 365 days).

So, the condition would be

if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))  
    printf("Year is a leap year\n");
else  
    printf("Not a leap year\n");
haccks
  • 104,019
  • 25
  • 176
  • 264
0

No. Every century year is NOT a leap year. Only those divisible by 400 are.

See http://en.wikipedia.org/wiki/Leap_year#Algorithm

No One in Particular
  • 2,846
  • 4
  • 27
  • 32