An evil number is a non-negative number that has an even number of 1s in its binary expansion. (Binary Expansion – is representation of a number in the binary numeral system or base-2 numeral system which represents numeric values using two different symbols: typically 0 (zero) and 1 (one)).
Odious Numbers: Numbers that are not Evil are called Odious Numbers. Given a number, the task is to check if it is Evil Number or Odious Numbers.
Examples :
Input : 3 Output : Evil Number Explanation: Binary expansion of 3 is 11, the number of 1s in this is 2 i.e even. Input : 16 Output : Odious Number(not an evil number) Explanation: Binary expansion of 16 = 10000, having number of 1s =1 i.e odd. Input : 23 Output : Evil Number Explanation: Binary expansion of 23 is 10111, the number of 1s in this is 4 i.e even.
C/C++
// C/C++ program to check if a number is // Evil number or Odious Number #include <iostream> using namespace std; #include <math.h> // returns number of 1s from the binary number int count_one( int n) { int c_one = 0; while (n != 0) { int rem = n % 10; // counting 1s if (rem == 1) c_one = c_one + 1; n = n / 10; } return c_one; } // Check if number is evil or not int checkEvil( int n) { int i = 0, bin = 0, n_one = 0; // converting n to binary form while (n != 0) { // calculating remainder int r = n % 2; // storing the remainders in binary // form as a number bin = bin + r * ( int )( pow (10, i)); n = n / 2; } // Calling the count_one function to count // and return number of 1s in bin n_one = count_one(bin); if (n_one % 2 == 0) return 1; else return 0; } // Driver Code int main( void ) { int i, check, num; num = 32; check = checkEvil(num); if (check == 1) cout << num << " is Evil Number\n" ; else cout << num << " is Odious Number\n" ; return 0; } // This code is contributed by Nikita Tiwari. |
Java
// Java program to check if a number is // Evil number or Odious Number class GFG { // returns number of 1s from the binary number static int count_one( int n) { int c_one = 0 ; while (n != 0 ) { int rem = n % 10 ; // counting 1s if (rem == 1 ) c_one = c_one + 1 ; n = n / 10 ; } return c_one; } // Check if number is evil or not static int checkEvil( int n) { int i = 0 , bin = 0 , n_one = 0 ; // converting n to binary form while (n != 0 ) { // calculating remainder int r = n % 2 ; // storing the remainders in binary // form as a number bin = bin + r * ( int )(Math.pow( 10 , i)); n = n / 2 ; } // Calling the count_one function to count // and return number of 1s in bin n_one = count_one(bin); if (n_one % 2 == 0 ) return 1 ; else return 0 ; } // Driver Code public static void main(String[] args) { int i, check, num; num = 32 ; check = checkEvil(num); if (check == 1 ) System.out.println(num + " is Evil Number" ); else System.out.println(num + " is Odious Number" ); } } /* This code is contributed by Mr. Somesh Awasthi */ |
Python
# Python program to check if a number is # Evil number or Odious number # returns number of 1s from the binary number def count_one(n): c_one = 0 while n ! = 0 : rem = n % 10 # Counting 1s if rem = = 1 : c_one = c_one + 1 n = n / 10 return c_one # Check if nnumber is evil or not def checkEvil(n): i = 0 binary = 0 # Converting n to binary form while n ! = 0 : r = n % 2 # Calculating Remainder # Storing the remainders in binary # form as a number binary = binary + r * ( int ( 10 * * i)) n = n / 2 # Calling the count_one function to count # and return number of 1s in bin n_one = count_one(binary) if n_one % 2 = = 0 : return True return False # Driver Code num = 32 check = checkEvil(num) if check: print num, "is Evil Nummber" else : print num, "is Odious Number" # Contributed by Harshit Agrawal |
C#
// C# program to check if a number is // Evil number or Odious Number using System; class GFG { // Returns number of 1s from // the binary number static int count_one( int n) { int c_one = 0; while (n != 0) { int rem = n % 10; // counting 1s if (rem == 1) c_one = c_one + 1; n = n / 10; } return c_one; } // Check if number is evil or not static int checkEvil( int n) { int i = 0, bin = 0, n_one = 0; // converting n to binary form while (n != 0) { // calculating remainder int r = n % 2; // storing the remainders in // binary form as a number bin = bin + r * ( int )(Math.Pow(10, i)); n = n / 2; } // Calling the count_one function to count // and return number of 1s in bin n_one = count_one(bin); if (n_one % 2 == 0) return 1; else return 0; } // Driver Code public static void Main(String[] args) { int check, num; num = 32; check = checkEvil(num); if (check == 1) Console.WriteLine(num + " is Evil Number" ); else Console.WriteLine(num + " is Odious Number" ); } } // This code is contributed by vt_m. |
PHP
<?php // PHP program to check if // a number is Evil number // or Odious Number // returns number of 1s // from the binary number function count_one( $n ) { $c_one = 0; while ( $n != 0) { $rem = $n % 10; // counting 1s if ( $rem == 1) $c_one = $c_one + 1; $n = $n / 10; } return $c_one ; } // Check if number // is evil or not function checkEvil( $n ) { $i = 0; $bin = 0; $n_one = 0; // converting n // to binary form while ( $n != 0) { // calculating remainder $r = $n % 2; // storing the remainders // in binary form as a number $bin = $bin + $r * (pow(10, $i )); $n = $n / 2; } // Calling the count_one // function to count and // return number of 1s in bin $n_one = count_one( $bin ); if ( $n_one % 2 == 0) return 1; else return 0; } // Driver Code $i ; $check ; $num ; $num = 32; $check = checkEvil( $num ); if ( $check == 1) echo $num , " is Evil Number\n" ; else echo $num , " is Odious Number\n" ; // This code is contributed by ajit. ?> |
Output :
32 is Odius Number
This article is contributed by Nikita Tiwari. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.