NCERT Computer Science Class #11 – Chapter #6 Solutions – Q #9

Chapter #6 – Flow of Control

NCERT Computer Science Class #11 – Chapter #6 Solutions – Q #9

Class 11 – Chapter 6 Solutions – PROGRAMMING EXERCISES #4

Write a program to check if the year entered by the user is a leap year or not



Python Program:

def is_leap_year(year):
    """Returns True if the year is a leap year, False otherwise."""
    # A year is a leap year if:
    # 1. It is divisible by 4
    # 2. If it is divisible by 100, it must also be divisible by 400
    if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
        return True
    else:
        return False

# Get user input
try:
    year = int(input("Enter a year: "))  # Accepting input as an integer
    if is_leap_year(year):
        print(f"{year} is a leap year.")
    else:
        print(f"{year} is not a leap year.")
except ValueError:
    print("Please enter a valid integer for the year.")

You can checkout this code from Github here – Programming Exercise – 4

Explanation of the Code:

1.    Function Definition:
      •	is_leap_year(year): This function takes an integer year as input and checks 
        the conditions for a leap year:
      •	A year is a leap year if it is divisible by 4.
      •	However, if it is divisible by 100, it must also be divisible by 400 to be 
        considered a leap year.
2.    User Input:
      •	The program prompts the user to enter a year and converts the input to an 
        integer.
      •	It uses try and except to handle invalid inputs gracefully.
3.    Checking Leap Year:
      •	The program calls the is_leap_year() function with the entered year and 
        prints whether it is a leap year or not based on the returned value.

Example Output:

Enter a year: 2024
2024 is a leap year.
Enter a year: 2023
2023 is not a leap year.

NCERT Computer Science Class 11 Chapter 6 Solutions Q 9

Posts created 29

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top