{"id":17350,"date":"2021-08-26T10:42:39","date_gmt":"2021-08-26T05:12:39","guid":{"rendered":"https:\/\/python-programs.com\/?p=17350"},"modified":"2021-11-22T18:37:11","modified_gmt":"2021-11-22T13:07:11","slug":"python-program-to-check-evil-number-or-not","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-check-evil-number-or-not\/","title":{"rendered":"Python Program to Check Evil Number or Not"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Determine Whether one String is a Rotation of Another<\/a> The Evil number is another unique positive whole number with an even number of 1s in its binary representation.<\/p>\n example:<\/p>\n 1) let n= 12<\/p>\n binary representation = 1100<\/p>\n # It has an even number of 1’s.\u00a0 Therefore 12 is an evil number.<\/p>\n 2) n= 4<\/p>\n binary representation = 100<\/p>\n It has an odd number of 1’s.\u00a0 Therefore 4 is not an evil number.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to check whether the given number is an evil number or not<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n 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 …<\/p>\n
\nEvil Number :<\/strong><\/p>\nGiven Number = 6<\/pre>\n
The given number 6 is an Evil Number<\/pre>\n
Given Number = 22<\/pre>\n
The given number 22 is Not an Evil Number<\/pre>\n
Program to Check Evil Number or Not<\/h2>\n
\n
Method #1:Using Built-in Functions (Static input)<\/span><\/h3>\n
\n
# Give the number as static input and store it in a variable.\r\ngvn_num = 9\r\n# Convert the given number into a binary number using the bin() function and\r\n# store it in another variable.\r\nk = bin(gvn_num)[2:]\r\n# Take a variable say 'c' and initialize its value with zero.\r\nc = 0\r\n# Loop in the above obtained binary number using the for loop.\r\nfor i in k:\r\n # Check if the iterator value is equal to '1' using the if conditional statement.\r\n if(i == '1'):\r\n # If the statement is true, increase the count value of the variable 'c' by 1 and\r\n # store it in the same variable.\r\n c += 1\r\n # Check for the even number of\u00a0 1's by c %2 is equal to zero or not using the\r\n# if conditional statement.\r\nif(c % 2 == 0):\r\n # If the statement is true, print \"The given number is an Evil Number\".\r\n print(\"The given number\", gvn_num, \"is an Evil Number\")\r\nelse:\r\n # Else print \"The given number is not an Evil Number\".\r\n print(\"The given number\", gvn_num, \"is Not an Evil Number\")\r\n<\/pre>\n
The given number 9 is an Evil Number<\/pre>\n
Method #2: Using Built-in Functions <\/span>(User Input)<\/h3>\n
\n
# Give the number as user input using the int(input()) function and store it in a variable.\r\ngvn_num = int(input(\"Enter some random number = \"))\r\n# Convert the given number into a binary number using the bin() function and\r\n# store it in another variable.\r\nk = bin(gvn_num)[2:]\r\n# Take a variable say 'c' and initialize its value with zero.\r\nc = 0\r\n# Loop in the above obtained binary number using the for loop.\r\nfor i in k:\r\n # Check if the iterator value is equal to '1' using the if conditional statement.\r\n if(i == '1'):\r\n # If the statement is true, increase the count value of the variable 'c' by 1 and\r\n # store it in the same variable.\r\n c += 1\r\n # Check for the even number of\u00a0 1's by c %2 is equal to zero or not using the\r\n# if conditional statement.\r\nif(c % 2 == 0):\r\n # If the statement is true, print \"The given number is an Evil Number\".\r\n print(\"The given number\", gvn_num, \"is an Evil Number\")\r\nelse:\r\n # Else print \"The given number is not an Evil Number\".\r\n print(\"The given number\", gvn_num, \"is Not an Evil Number\")\r\n<\/pre>\n
Enter some random number = 3\r\nThe given number 3 is an Evil Number<\/pre>\n
\n