Thursday, 5 December 2024

Day 13 : Python Program to Check whether a given year is a Leap Year

 



def is_leap_year(year):
    
    if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
        return True
    else:
        return False

year = int(input("Enter a year: "))
if is_leap_year(year):
    print(f"{year} is a leap year.")
else:
    print(f"{year} is not a leap year.")

Code Explanation:

  1. Function Definition:


    def is_leap_year(year):
    • A function is_leap_year is defined, which takes one argument: year.
  2. Leap Year Logic:

    if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
           return True
            else:
           return False
    • Leap Year Rules:
      • A year is a leap year if:
        1. It is divisible by 4 and not divisible by 100.
        2. Or, it is divisible by 400.
    • Explanation of Conditions:
      • year % 4 == 0: The year is divisible by 4.
      • year % 100 != 0: The year is not divisible by 100 (to exclude years like 1900, 2100 which are divisible by 100 but not leap years).
      • year % 400 == 0: The year is divisible by 400 (e.g., 2000, 2400 which are leap years).
    • If either condition is true, the function returns True (indicating a leap year), otherwise False.

  1. Input:

      year = int(input("Enter a year: "))
    • The program prompts the user to input a year, which is converted to an integer and stored in the variable year.
  2. Check Leap Year:

    if is_leap_year(year):
    print(f"{year} is a leap year.")
           else:
           print(f"{year} is not a leap year.")
    • The function is_leap_year is called with the input year.
    • Depending on whether the function returns True or False:
      • If True: The year is printed as a leap year.
      • If False: The year is printed as not a leap year.

    #source code --> clcoding.com 

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (28) AI (33) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (173) C (77) C# (12) C++ (82) Course (67) Coursera (223) Cybersecurity (24) data management (11) Data Science (127) Data Strucures (8) Deep Learning (20) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (59) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (1) Pandas (4) PHP (20) Projects (29) Python (923) Python Coding Challenge (318) Python Quiz (4) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (42) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses