Wednesday 17 January 2024

Check whether a given year is a leap year or not

There are 3 ways to detect if it is a leap year or not

A leap year is a year that is evenly divisible by 4, except for years that are divisible by 100. However, years divisible by 400 are leap years again.

Method 1


if year % 4 == 0: 
     if year % 100 == 0:
         if year % 400 == 0: 
             print("Leap year") 
         else: 
             print("Not leap year") 
    else: 
         print("Leap year") 
else: 
    print("Not leap year")



Method 2:

year = int(input("Enter a year: ")) result = "Leap year" if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0) else "Not leap year" print(result)

Method 3:

year = int(input("Enter a year: ")) if year % 400 == 0: result = "Leap year" elif year % 100 == 0: result = "Not leap year" elif year % 4 == 0: result = "Leap year" else: result = "Not leap year" print(result)

No comments:

Post a Comment

How to connect to VPS with Private Key on Mac OS

1. Use the cd command to navigate to the directory where your private key file is stored. For example, if your private key is in the Downlo...