How to write a script to determine whether or not a given year is a leap-year

26 views (last 30 days)
I've made two attempts to writing this code but neither of them work correctly, I've done a lot of reading about this and I know I can just copy and paste another code from an online forum but I was hoping to learn it my own way. I've also read through the help option in matlab for if, elseif, and else but I am still struggling to understand: exactly what they do, when they should be used, and the order in which they need to be nested. A greater understanding of this should make writing this code a breeze! If it helps you to understand my dummy variables, my first attempt at this code was actually the 2nd portion of code.
year = input('input a year\n')
first = 100;
second = 4;
third = 400;
if rem(year,second) == 0
if rem(year,third) == 0
if rem(year,first) == 0
fprintf('%g is not a leap-year :(((( \n',year)
else
fprintf('%g is a leap year!! :D \n',year)
end
else
fprintf('%g nope not a leap year',year)
end
else
fprintf('nah, not a leap year boi')
end
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if rem(year,first) == 0
fprintf('%g is not a leap-year :(((( \n',year)
elseif rem(year,second) == 0
if rem(year,third) == 0
fprintf('%g is a leap year!! :D \n',year)
else
fprintf('%g is not a leap year awweee:,(',year)
end
end
  4 Comments
David Goodmanson
David Goodmanson on 29 Aug 2017
Hi Will,
Checking for the not-divisible condition certainly works, but if you want to stay with checking for divisibility (personally I find that method easier to follow), you just have to modify your order of if checks. If you check for divisibility by 100 first, the year still may or may not be divisible by 400 so you can't decide anything at that stage. But if you check for divisibility by 400 first, you know the answer. And so forth. So you just need to check for divisibility in decreasing order of divisors, with appropriate elseifs in between.

Sign in to comment.

Accepted Answer

James Tursa
James Tursa on 29 Aug 2017
Edited: James Tursa on 29 Aug 2017
You are not applying the tests in the proper order. From this link:
https://en.wikipedia.org/wiki/Leap_year
Look at the Algorithm section and you see this:
if (year is not divisible by 4) then (it is a common year)
else if (year is not divisible by 100) then (it is a leap year)
else if (year is not divisible by 400) then (it is a common year)
else (it is a leap year)
Just copy and paste that into your code and turn it into m-code. E.g.,
if (year is not divisible by 4)
(it is a common year)
elseif (year is not divisible by 100)
(it is a leap year)
elseif (year is not divisible by 400)
(it is a common year)
else
(it is a leap year)
end
Just replace all that stuff in parentheses with your appropriate rem code and fprintf code.

More Answers (2)

Tahmina Tabassum Treena
Tahmina Tabassum Treena on 20 Jan 2021
It's so easy.Can be executed in one line.By this logic,you can print it out.
mod(year,4) == 0 && (mod(year,100) ~= 0 || mod(year,400) == 0)

VIGNESH V
VIGNESH V on 5 Aug 2021
prompt = 'enter the year which you want to check whether it is leap year or not\n';
year = input(prompt);
cond1 = 100;
cond2 = 4;
cond3 = 400;
if(((rem(year,cond2)==0) && ( rem(year,cond1)~=0)) || (rem(year,cond3)==0))
fprintf("entered year %d is a leap year\n",year);
else
fprintf("it is not a leap year:\n");
end
there are two combinations of conditions available for a year to be a leap year.
first combination of condition - (year and 4 must be completely divisible) and (year and 100 must not be divisible)
second combination of condition - (year and 400 must be completely divisible)
if first combination of condition fails, atleast second combination of condition should be true.

Community Treasure Hunt

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

Start Hunting!