my question is can anyone improve my code to get the right answer?

2 visualizzazioni (ultimi 30 giorni)
Design and implement a Matlab script that reads an integer value representing a year from the user. The purpose of the program is to determine if the year is a leap year (and therefore has 29 days in February) in the Gregorian calendar. A year is a leap year if it is divisible by 4, unless it is also divisible by 100 but not 400.
For example, the year 2003 is not a leap year, but 2004 is. The year 1900 is not a leap year because it is divisible by 100, but the year 2000 is a leap year because though it is divisible by 100, it is also divisible by 400.
Produce an error message for any input value less than 1582 (the year the Gregorian calendar was adopted).
clear;
clc;
year = input(' enter a year ')
if year >= 1582
if mod(year,100) == 0
msg2 = [num2str(year), ' is not a leap year.']
if mod(year,400) == 0
msg2 = [num2str(year), ' is a leap year.']
elseif mod(year,4) == 0
msg2 = [num2str(year), ' is a leap year.']
end
end
else
msg =['ERROR: ',num2str(yr) ' is before the Gregorian Calendar.']
end
disp(msg);
disp(msg2);
disp(msg3);
disp(msg4);

Risposte (1)

Gareth Lee
Gareth Lee il 21 Ott 2016
It seems like to decide given year is whether or not a leap year. If you use the matlab 2016b, you can use the function 'leapyear' to obtain the result your want.

Community Treasure Hunt

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

Start Hunting!

Translated by