How to write function that recognizes whether a year (e.g2010) input by user is leap year or not

Write a function that accepts a year as input and determines whether it is a leap year. The function should return 1 if the year is a leap year, otherwise return 0. Your function should start as follows:
function out = is_leap(year)
The rules for determining leap years are as follows:
1) Leap Years are any year that can be evenly divided by 4 (such as 2012, 2016, etc)
2) except if it can can be evenly divided by 100, then it isn't (such as 2100, 2200, etc)
3) except if it can be evenly divided by 400, then it is (such as 2000, 2400)
HINT: You may want to use the mod function and if statements.

Réponses (1)

I recommend you put matlab's 'mod' function to good use. Here is one way:
t = mod(y,4)==0 & (mod(y,100)~=0 | mod(y,400)==0);

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by