# What is an evil number – Python Program to Check Evil Number or Not

What is an evil number: In the previous article, we have discussed Python Program to Determine Whether one String is a Rotation of Another
Evil Number :

The Evil number is another unique positive whole number with an even number of 1s in its binary representation.

example:

1) let n= 12

binary representation = 1100

# It has an even number of 1’s.  Therefore 12 is an evil number.

2) n= 4

binary representation = 100

It has an odd number of 1’s.  Therefore 4 is not an evil number.

Examples:

Example1:

Input:

Given Number = 6

Output:

The given number 6 is an Evil Number

Example2:

Input:

Given Number = 22

Output:

The given number 22 is Not an Evil Number

## Program to Check Evil Number or Not

Below are the ways to check whether the given number is an evil number or not

### Method #1:Using Built-in Functions (Static input)

Approach:

• Give the number as static input and store it in a variable.
• Convert the given number into a binary number using the bin() function and store it in another variable.
• Take a variable say ‘c’ and initialize its value with zero.
• Loop in the above obtained binary number using the for loop.
• Check if the iterator value is equal to ‘1’ using the if conditional statement.
• If the statement is true, increase the count value of the variable ‘c’ by 1 and store it in the same variable.
• Check for the even number of  1’s by c %2 is equal to zero or not using the if conditional statement.
• If the statement is true, print “The given number is an Evil Number”.
• Else print “The given number is not an Evil Number”.
• The Exit of the program.

Below is the implementation:

# Give the number as static input and store it in a variable.
gvn_num = 9
# Convert the given number into a binary number using the bin() function and
# store it in another variable.
k = bin(gvn_num)[2:]
# Take a variable say 'c' and initialize its value with zero.
c = 0
# Loop in the above obtained binary number using the for loop.
for i in k:
# Check if the iterator value is equal to '1' using the if conditional statement.
if(i == '1'):
# If the statement is true, increase the count value of the variable 'c' by 1 and
# store it in the same variable.
c += 1
# Check for the even number of  1's by c %2 is equal to zero or not using the
# if conditional statement.
if(c % 2 == 0):
# If the statement is true, print "The given number is an Evil Number".
print("The given number", gvn_num, "is an Evil Number")
else:
# Else print "The given number is not an Evil Number".
print("The given number", gvn_num, "is Not an Evil Number")


Output:

The given number 9 is an Evil Number

### Method #2: Using Built-in Functions (User Input)

Approach:

• Give the number as user input using the int(input()) function and store it in a variable.
• Convert the given number into a binary number using the bin() function and store it in another variable.
• Take a variable say ‘c’ and initialize its value with zero.
• Loop in the above obtained binary number using the for loop.
• Check if the iterator value is equal to ‘1’ using the if conditional statement.
• If the statement is true, increase the count value of the variable ‘c’ by 1 and store it in the same variable.
• Check for the even number of  1’s by c %2 is equal to zero or not using the if conditional statement.
• If the statement is true, print “The given number is an Evil Number”.
• Else print “The given number is not an Evil Number”.
• The Exit of the program.

Below is the implementation:

# Give the number as user input using the int(input()) function and store it in a variable.
gvn_num = int(input("Enter some random number = "))
# Convert the given number into a binary number using the bin() function and
# store it in another variable.
k = bin(gvn_num)[2:]
# Take a variable say 'c' and initialize its value with zero.
c = 0
# Loop in the above obtained binary number using the for loop.
for i in k:
# Check if the iterator value is equal to '1' using the if conditional statement.
if(i == '1'):
# If the statement is true, increase the count value of the variable 'c' by 1 and
# store it in the same variable.
c += 1
# Check for the even number of  1's by c %2 is equal to zero or not using the
# if conditional statement.
if(c % 2 == 0):
# If the statement is true, print "The given number is an Evil Number".
print("The given number", gvn_num, "is an Evil Number")
else:
# Else print "The given number is not an Evil Number".
print("The given number", gvn_num, "is Not an Evil Number")


Output:

Enter some random number = 3
The given number 3 is an Evil Number

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.