Create function to calculate leap year

71 views (last 30 days)
Greg
Greg on 31 Aug 2011
Commented: Hardik Senta on 28 Mar 2022
Hi I need to create a function that determines a leap year with the conditions
function result = is_leap_year(year)
% IS_LEAP_YEAR Determine if a given year is a leap year
% RESULT = IS_LEAP_YEAR(YEAR) returns TRUE if YEAR is a leap year, and
% FALSE otherwise.
% An error is raised if YEAR is not a positive integer.
a year is a leap year if it is divisible by four, unless it is also divisible by 100, unless it is also divisible by 400. I have been able to create a function however cannot work out how to do the false otherwise statement. Also with true and false I get 1(true) and 0 (false). Is this acceptable? Here's my function
function [ status ] = leapyear( year )
if mod(year, 400) == 0
status = true;
elseif mod(year, 4) == 0 && mod(year, 100) ~= 0
status = true;
else
status = false;
end
end
  5 Comments
chantsal bymba
chantsal bymba on 7 Nov 2020
How to describe it
(Write a script which calls your function with a range from [1952 to 2020] and displays the output.)
Walter Roberson
Walter Roberson on 8 Nov 2020
years = 1795:1810;
for yidx = 1 : length(years)
result(yidx) = leapyear(years(yidx));
end

Sign in to comment.

Answers (8)

Fangjun Jiang
Fangjun Jiang on 31 Aug 2011
TF=~(datenum(year,2,29)==datenum(year,3,1))
Using your approach, it should be like below. This is primarily for learning the if-else statement using only the necessary condition checking.
function [ status ] = leapyear( year )
if mod(year, 4) == 0
status = true;
if mod(year, 100) == 0
status=false;
if mod(year,400)==0
status = true;
end
end
else
status=false;
end
true and false are displayed as 1 and 0, but they are different than the double precision number 1 and 0. They are logical. Type this to find out.
class(true)
class(false)
class(1)
class(0)
To make it work for vector, I recommend Andrei's approach.
>> ly=@(yr)~rem(yr,400)|rem(yr,100)&~rem(yr,4);
>> ly(1999:2005)
ans =
0 1 0 0 0 1 0
  4 Comments
Fangjun Jiang
Fangjun Jiang on 31 Aug 2011
All right, I'll play alone...
T=datenum(y,2,29)~=datenum(y,3,1);
Walter Roberson
Walter Roberson on 31 Aug 2011
Renaming variables doesn't count in golfing, not until you need more than 26 variables (in which case it would be fair game to code so that the variables used most frequently had the shorter names.)
2 digit year numbers are considered to be within the current century in string representations, not in numeric representations.
The terms of the original assignment called for the function to error if the year was not a positive integer; your code does not make that test. Your code will return 1 (true) for an input of 0 -- but of course there was no year 0 so it could not have been a leap year.

Sign in to comment.


Paulo Silva
Paulo Silva on 31 Aug 2011
Take a look at this ISLEAP Function , also look at Jan Simon comment to that file.

Rebecca
Rebecca on 31 Aug 2011
Hi - @Jack, I have the same assignment (we might have class together)
I think I must have some settings weird, when I type
function [ status ] = leapyear (year)
Error: Function definitions are not permitted in this context.
...and would always error when year = 23.
Error using ==> year at 23
Please enter D.
I figured it seemed to think year was to do with the date and clock so I substituted my own word for practise (star, beep, whatever), but then it wanted me to define this word before I started my input in the first place. I know you guys are down to the core pieces of the assignment, but I could use some baby step help.
I'm also pretty sure we're meant to use the rem function, but from all the research I've done so far, mod seems to work similarly - if not better, anyone want to explain?
  4 Comments
Walter Roberson
Walter Roberson on 31 Aug 2011
function [ status ] = leapyear (year)
and the rest of your code that implements the test for any one year should go in leapyear.m
The parts of the file that you had _before_ the "function" line should stay where they were... unless, that is, your original file happened to be named leapyear.m in which case you need to choose a different name for the parts before the "function" line.
Rebecca
Rebecca on 1 Sep 2011
fantastic! - I had a friend help and learnt the difference between the scrip screen etc. (thank you!)
I added an extra line in to the above to make sure that year was an integer:
if year <=0 || rem(year,1) ~=0
error('either not positive or non integer')
end
(next I'll learn how to make it look light grey and code for this site)

Sign in to comment.


Walter Roberson
Walter Roberson on 31 Aug 2011
Julian Calendar established 46 BC, but leap years not properly added up through 7 CE
8 CE to 1581 CE - every year divisible by 4 was a leap year; the Julian Calendar.
1582 CE - Gregorian Calendar decreed, with the 4 except 100 except-except 400 structure. Some countries adopt this in October 1582 CE
1582 CE onward - various countries adopt at different times. Some of the changes were strange; some areas went back to the Julian calendar
1752 - Britain and colonies adopt Gregorian Calendar in September 1752 CE
1752 CE onward - more changes in various countries
1912 CE - 1928 CE: Republic of China is officially Gregorian but in practice local rules are followed
1918 CE Russia becomes second last major country to change. The "October Revolution" of 1917 was October in Julian calendar but November in Gregorian
1926 CE Turkey becomes last western country to change.
1929 CE Republic of China goes officially Gregorian again
The next 700 years: arguments from the Orthodox Christian churches as they have not adopted the Gregorian Calendar for some purposes; it will next make a difference in 2800 CE...

Fahad alshammari
Fahad alshammari on 21 Oct 2018
Edited: Walter Roberson on 11 Jun 2019
function year
year = input (' Enter the year : ')
if mod(year,4) == 0 disp ('true')
elseif mod(year,100) == 0 disp ('falus')
elseif mod(year,400) == 0 disp ('true')
else disp ('falus')
end end
  3 Comments
Walter Roberson
Walter Roberson on 21 Oct 2018
Reverse the order of your tests. Multiple of 400 is a leap year, multiple of 100 is not a leap year unless you already passed the 400 test, multiple of 4 is a leap year unless you already failed the 100 test.

Sign in to comment.


Shubham Agrawal
Shubham Agrawal on 11 Jun 2019
a=input('Enter the year');
if (((a/400-floor(a/400))==0) | (((a/4-floor(a/4))==0)&(a/100-floor(a/100)~=0)))==1
disp('This is a leap year');
else
disp('This is not a leap year');
end

Shanjul Shrivastava
Shanjul Shrivastava on 11 Jun 2020
Edited: Shanjul Shrivastava on 11 Jun 2020
function result = is_leap_year(year)
if year<=0; error("year is not a positive integer");
elseif(mod(year,4))~=0 | (mod(year,4)==0 & mod(year,100)==0 & mod(year,400)~=0)
result="false";
else
result="true";
end
end
  4 Comments
madhan ravi
madhan ravi on 11 Jun 2020
There’s an inbuilt function for true & false
Walter Roberson
Walter Roberson on 11 Jun 2020
The string() object "true" is not the same thing as TRUE, which is a logical value.
Also, there are no circumstances under which the code is expected to error(). Years before 46 BCE are not leap years. See the list at https://www.mathworks.com/matlabcentral/answers/14859-create-function-to-calculate-leap-year#answer_20236

Sign in to comment.


Dongchi Jiang
Dongchi Jiang on 17 Feb 2022
Edited: Dongchi Jiang on 17 Feb 2022
clc;
year=input('Enter a year: \n');
if (rem(year,400)==0 | rem(year,4)==0 & rem(year,100)~=0)
disp('The year is a leap year')
else
disp('The year is not a leap year')
end

Categories

Find more on MATLAB 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!