Number of leap years in a range

1 次查看(过去 30 天)
I have created
startYear=input('What is the start year? ');
endYear=input('What is the end year? ');
years=(startYear:endYear);
and from that I am supposed to create a program that takes each year in the vector and determine whether or not that year is a leap year and then, using logical vectors, add the result to get how many leap years there are in the given vector.
I already have the if statements for determing whether or not 1 year is a leap year, but I can't figure out how to do it with more than one.

回答(1 个)

Yasasvi Harish Kumar
Hi,
The following should be able to solve your problem. The variable count stores the number of leap years.
startYear=input('What is the start year? ');
endYear=input('What is the end year? ');
years=(startYear:endYear);
count = 0
for i = 1:length(years)
if (mod(years(i),4) == 0)
count = count + 1;
end
end
Regards
  1 个评论
Walter Roberson
Walter Roberson 2019-2-15
This fails for year 1800 1900 2100 2200 2300 2500 2600 2700 2900 3000 3100 3300 ...
Whether it fails for 1700 depends exactly where in the world you are referring to . In the UK 1700 was a leap year but in parts of Europe it was not.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by