write a program which uses a function to test if a given year is a leap year. your function should return a value of 0 if it is true that the year is a leap year and a value of 1 if it is false. in matlab

main.m
function isLeapYear = checkLeapYear(year)
% A function to check if a given year is a leap year
% If it's true that the year is a leap year, return 0
% Else, return 1 

if mod(year, 4) == 0 && (mod(year, 100) ~= 0 || mod(year, 400) == 0)
    isLeapYear = 0;
else
    isLeapYear = 1;
end
286 chars
11 lines

This function takes in one argument, which is the year to be checked. The modulo operator is used to determine if the year is divisible by 4, 100, and 400. If the year is divisible by 4 and either not divisible by 100 or divisible by 400, then it is a leap year and the function returns 0. Otherwise, the function returns 1.

To call this function and check if a year is a leap year or not, you can simply enter the year as an input argument in the command window:

main.m
>> checkLeapYear(2000)
ans = 
   0
>> checkLeapYear(2001)
ans = 
   1
70 chars
7 lines

gistlibby LogSnag