HackerRank Python: Loops Solution

Table of Contents

Question

Task
The provided code stub reads and integer,n , from STDIN. For all non-negative integers , i < n, print i2 . 

Example

n = 3 

The list of non-negative integers that are less than n = 3  is [ 0, 1, 2 ]. Print the square of each number on a separate line.

0
1
4

Input Format

The first and only line contains the integer, .

Constraints

1 ≤ n ≤ 20

Output Format

Print  lines, one corresponding to each .

Sample Input 0

5

Sample Output 0

0
1
4
9
16

Solution

				
					def is_leap(year):
    if year % 4 == 0:  # Check if the year is divisible by 4
        if year % 100 == 0:  # Check if the year is divisible by 100
            if year % 400 == 0:  # Check if the year is divisible by 400
                return True  # If divisible by 400, it is a leap year
            else:
                return False  # If divisible by 100 but not by 400, it's not a leap year
        else:
            return True  # If divisible by 4 but not by 100, it's a leap year
    else:
        return False  # If not divisible by 4, it's not a leap year

# Read the input year
year = int(input())

# Call the is_leap function and print the result
print(is_leap(year))

				
			

In this code, we define the is_leap function that takes the year as input and checks the three conditions to determine whether it’s a leap year or not. We use the modulo operator (%) to check for divisibility.

Note: The code assumes that the input year is within the given constraints (1900 ≤ year ≤ 10^5).

If you find anything wrong in this Solution, feel free to reach us in the comment section.

Sharing Is Caring:

Leave a Comment