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

 Réponse acceptée

Image Analyst
Image Analyst le 22 Nov 2015

0 votes

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.

1 commentaire

Image Analyst
Image Analyst le 22 Nov 2015
By the way, there are other ways like any(), switch, ismember(), etc. that you can use if you want it more compact.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Startup and Shutdown dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by