Python program to check a number n is weird or not


Suppose we have a number n. We shall have to check whether n is weird or not. Here a number is weird when − 1. The number is odd 2. The number is not in range 2 to 5 3. The number is even and in range 6 to 20

So, if the input is like n = 18, then the output will be Weird because it is even and in range 6 to 20.

To solve this, we will follow these steps −

  • if n is odd, then
    • return "Weird"
  • otherwise when (n > 1 and n < 6) or n > 20, then
    • return "Not Weird"
  • otherwise when n > 6 and n < 21, then
    • return "Weird"

Example

Let us see the following implementation to get better understanding −


def solve(n):
    if n & 1:
        return "Weird"
    elif (n > 1 and n < 6) or n > 20:
        return "Not Weird"
    elif n > 6 and n < 21:
        return "Weird"
   
n = 18
print(solve(n))

Input

18

Output

Weird

Updated on: 06-Oct-2021

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements