MATLAB: Number of Days in a Month

fprintfif statementmod

I have a little problem with my function, which shows the user the number of days when any month and any year is input. Here is an image of the leapyear function
if mod(year,400) == 0
result=1;
elseif mod(year,100) == 0
result=0;
elseif mod(year,4) == 0
result=1;
else
result=0;
end
When the month=4 and the year=2015, the results displayed are still 31. Why is that? Am i missing something?
month=input('enter any month:\n ');
year=input('enter any year:\n ');
result=leap(year);
if (month<1) || (month>12)
fprintf('month ERROR\n');
elseif month==2 && result==0;
fprintf('28\n');
elseif month==2 && result==1;
fprintf('29\n');
elseif month==1||3||5||7||8||10||12;
fprintf('31\n');
elseif month==4||6||9||11;
fprintf('30\n');
Thanks in advance

Best Answer

You can't do multiple ORs like this:
elseif month==1||3||5||7||8||10||12;
You must do it separately for each comparison:
elseif theMonth==1||theMonth==3||theMonth==5||theMonth==7||theMonth==8||theMonth==10||theMonth==12;
Also, DO NOT USE month as the name of your variable since it's the name of a built-in function, and you'll override it. It is not advised to override the functionality of any built-in MATLAB function when all you need to do is to pick a different variable name, like I did above.
Related Question