Number of leap years in a range

조회 수: 1 (최근 30일)
Lilly O'Brien
Lilly O'Brien 2019년 2월 15일
댓글: Walter Roberson 2019년 2월 15일
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
Yasasvi Harish Kumar 2019년 2월 15일
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