-3

Create function is_leap_year(y) in which y is a year number. the function will return True if the year is a leap year, otherwise, it will return False. A leap year's number is an integer multiple of 4 (except for years evenly divisible by 100, which are not leap years unless evenly divisible by 400)

  • Does this answer your question? [Leap year calculation](https://stackoverflow.com/questions/725098/leap-year-calculation) – Ole V.V. May 23 '22 at 11:26
  • Welcome to Stack Overflow. Please learn that you are supposed to search before asking a question here and when you post a question, tell us what your search brought up and specify how it fell short of solving your problem. It’s for your own sake since (1) you often find a better answer faster that way (2) it allows us to give preciser and more focused answers. It also tends to prevent or at least reduce the number of downvotes. – Ole V.V. May 23 '22 at 11:27

2 Answers2

0

That's a fairly succint (pseudo-code):

define is_leap_year(y):
    if y is divisible by 400:
        return true
    if y is divisible by 100:
        return false
    if y is divisible by 4:
        return true
    return false

That's probably the easiest solution as doing it in that order allows you to avoid complicated (and totally unnecessary) if/else "ladder" code:

define is_leap_year(y):
    if y is divisible by 4:
       if y is divisible by 100:
           if y is divisible by 400:
               return true
           else:
               return false
        else:
            return true
    else:
        return false

Now just translate that (the first one, above) to your language of choice.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

Leap year: Happens every 4 years, except for end-of-century years, which must be divisible by 400.

  • end-of-century years (ending with 00) like 1000, 1100, 1200, ...., 2000, 2100, 2200, ...
  • divisible by 100 means century year and divisible by 400 (leap year)
  • not divisible by 100 means not century year and divisible by 4 (leap year)
  • neither divisible by 400 and 4 (not leap year)

Code Snippet

def is_leap_year(y):
   if (y % 400 == 0) and (y % 100 == 0): return True
   elif (y % 4 == 0) and (y % 100 != 0): return True
   else: return False
Nayan
  • 471
  • 2
  • 9
  • 1
    Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney May 24 '22 at 00:13