HackerRank Day 3 Solution in Python: Intro to Conditional Statements

Today we will see the HackerRank Day 3 Solution in Python. The problem is named Intro to Conditional Statements which is part of 30 Days of code on HackerRank. Let’s get started!

Day 11: Intro to Conditional Statements Problem statement

We are given an integer n, our task is to perform the following conditional actions

  • If n is odd, print Weird
  • If n is even and in the inclusive range of 2 to 5, print Not Weird
  • If n is even and in the inclusive range of 6 to 20, print Weird
  • If n is even and greater than 20, print Not Weird

Sample Input

3

Sample Output

Weird

Explanation: The input integer n is odd and by the given problem statement odd numbers are weird, so we print Weird.

You can solve the problem here.

HackerRank Day 3 Solution in Python

#!/bin/python3

import math
import os
import random
import re
import sys

if __name__ == '__main__':
    #Get user input
    N = int(input())
    #If n is odd
    if N%2!=0:
        print("Weird")
    #If n is even
    elif N%2==0:
        #If n greater than 2 and lesser than 5
        if N>=2 and N<=5:
            print("Not Weird")
        #If n greater than 6 and lesser than 20
        if N>=6 and N<=20:
            print("Weird")
        #If n greater than 2 and lesser than 100
        elif N>=20 and N<=100:
            print("Not Weird")

Code Explanation

  • After getting user input, we check if n is odd, then print weird
  • Else if n is even, then check if n is greater than 2 and lesser than 5, then print Not Weird
  • Then check if n is greater than 6 and lesser than 20, then print Weird
  • Then check if n is greater than 20 and lesser than 100, then print Not Weird

Also Read:

Share:

Author: Keerthana Buvaneshwaran