Number of leap years in a range

1 view (last 30 days)
Lilly O'Brien
Lilly O'Brien on 15 Feb 2019
Commented: Walter Roberson on 15 Feb 2019
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.

Answers (1)

Yasasvi Harish Kumar
Yasasvi Harish Kumar on 15 Feb 2019
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 Comment
Walter Roberson
Walter Roberson on 15 Feb 2019
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.

Sign in to comment.

Categories

Find more on Dates and Time in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!