Categories
Computer Science

Solving Problem: Leap Year

(If-elif-else Control Flow)

Check if a year is a leap year.

 As per the Gregorian calendar (in 1582), the following rule is used to determine the kind of year:

  • If the year number isn’t divisible by four, it’s a common year.
  • Otherwise, if the year number isn’t divisible by 100, it’s a leap year.
  • Otherwise, if the year number isn’t divisible by 400, it’s a common year.
  • Otherwise, it’s a leap year.

The task is to determine if the given year is a leap year or not.

Output messages:

“Leap year.” if the year is a leap year.

“Common year.” if the year is common.

“Not within the Gregorian era.” If the year falls out of the Gregorian era.

Input: 2000

Output: Leap year.

Input: 1999

Output: Common year.

Input: 1996

Output: Leap year.

Input: 1500

Output: Not within the Gregorian era.

Python
year = int(input("Enter a year: "))
#
# Write your code here.
#
if year > 1582:
if (year%4 == 0 and year%100 != 100) or year%400 == 400:
print("Leap year.")
else:
print("Common year.")
else:
print("Not within the Gregorian era.")

Output:

Priyanka B.'s avatar

By Priyanka B.

Hello and welcome to my little corner of internet!! I am a techie. I am very interested to discover and innovate new advances in science and technology. Blogging is one of my hobbies which I think is very useful for broadening my knowledge horizons and help me grow my skills. Apart from blogging I have also little taste in artistic skills and literature, which can keep my writing and posts tangy.

Whether you stumbled in by chance or came here on purpose, I hope you find something that sparks your curiosity or makes you think a little deeper. Thanks for stopping by!!

Leave a comment