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

2 次查看(过去 30 天)
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);

回答(1 个)

Gareth Lee
Gareth Lee 2016-10-21
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.

类别

Help CenterFile Exchange 中查找有关 Calendar 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by