# If else condition to determine if a year is a leap year

92 views (last 30 days)

Show older comments

### Accepted Answer

Jos (10584)
on 7 Mar 2014

function Days = GetDays (Y)

if isLeapYear(Y)

Days = 365 ;

else

Days = 366 ;

end

Now your problem is to write second function IsLeapYear that returns true for leap years. (hint: see Nitin's answer)

### More Answers (3)

Nitin
on 7 Mar 2014

- The year is evenly divisible by 4;
- If the year can be evenly divided by 100, it is NOT a leap year, unless;
- The year is also evenly divisible by 400. Then it is a leap year.

I have done the first part, I'll leave the rest to you to implement

if mod(year,4)== 0 % use the modulo operator to check for remainder

days == 366

else

days == 365

end

Shikhar Srivastava
on 28 Mar 2020

Edited: Shikhar Srivastava
on 28 Mar 2020

if(year/4==0 || year/400==0 && ~((year/100==0)))

##### 4 Comments

Rik
on 31 Mar 2020

You are using division. Consider the year 2004, which was a leap year. What happens for each of your checks?

year/4==0% year/4 is 501, so this returns false

year/400==0% year/400 is 5.01, so this returns false

~(year/100==0)% year/100 is 20.04, so this returns true

%so your if is this:

if false || (false && true)

If you describe it with words, what is the condition to check if a year is a leap year?

### See Also

### Categories

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!