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

Mức thuế ở Úc từ 07/2024

Chính phủ Úc đã thay đổi các mức thuế suất cho thu nhập cá nhân và các mức thu nhập (được gọi là ‘ngưỡng’) áp dụng những thuế suất này. Việc...