深度阅读

How to Program to Check Leap Year in python

作者
作者
2023年08月22日
更新时间
10.92 分钟
阅读时间
0
阅读量

To check if a year is a leap year in Python, you can use the following program:

year = int(input("Enter a year: "))

if year % 4 == 0:
    if year % 100 == 0:
        if year % 400 == 0:
            print(year, "is a leap year")
        else:
            print(year, "is not a leap year")
    else:
        print(year, "is a leap year")
else:
    print(year, "is not a leap year")

In this program, we first prompt the user to enter a year using the input() function, and then convert the input to an integer using the int() function. We then use a nested if-else statement to check if the year is a leap year. According to the algorithm, a year is a leap year if it is divisible by 4 but not divisible by 100, or if it is divisible by 400. We use the modulo operator % to check if the year is divisible by 4, 100, or 400.

This program will print whether the given year is a leap year or not based on the input provided by the user.

Note that the algorithm to determine leap year status might vary in different calendars, but this program applies to Gregorian calendar.

博客作者

热爱技术,乐于分享,持续学习。专注于Web开发、系统架构设计和人工智能领域。