Open In App

Evil Number

Last Updated : 19 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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++ 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 */


Python3




# 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 number 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 Number")
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.
?>


Javascript




<script>
 
// Javascript program to check if a number
// is Evil number or Odious Number
 
// Returns number of 1s from
// the binary number
function count_one(n)
{
    let c_one = 0;
     
    while (n != 0)
    {
        let rem = n % 10;
 
        // Counting 1s
        if (rem == 1)
            c_one = c_one + 1;
             
        n = parseInt(n / 10, 10);
    }
    return c_one;
}
 
// Check if number is evil or not
function checkEvil(n)
{
    let i = 0, bin = 0, n_one = 0;
 
    // Converting n to binary form
    while (n != 0)
    {
         
        // Calculating remainder
        let r = n % 2;
 
        // Storing the remainders in
        // binary form as a number
        bin = bin + r * (Math.pow(10, i));
        n = parseInt(n / 2, 10);
    }
 
    // 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
let check, num;
num = 32;
check = checkEvil(num);
 
if (check == 1)
  document.write(num + " is Evil Number");
else
  document.write(num + " is Odious Number");
   
// This code is contributed by suresh07  
 
</script>


Output

32 is Odious Number

Time Complexity: O(log2n)
Auxiliary Space: O(1), As constant extra space is used.

With the use of __builtin_parity(x): This function is used to check the parity of a number. This function returns true(1) if the number has odd parity else it returns false(0) for even parity. 

C++




#include <iostream>
using namespace std;
 
int main()
{
    int num = 5;
    if (__builtin_parity(num))
        cout << num << " is Odious Number\n";
    else
        cout << num << " is Evil Number\n";
    return 0;
}


Java




import java.lang.*;
 
public class Main {
    public static void main(String[] args) {
        int num = 5;
        if (Integer.bitCount(num) % 2 == 1)
            System.out.println(num + " is Odious Number");
        else
            System.out.println(num + " is Evil Number");
    }
}
// This code is contributed by shiv1o43g


Python3




# Python code addition
 
num = 5
if bin(num).count('1') % 2 == 1:
    print(num, "is Odious Number")
else:
    print(num, "is Evil Number")
 
# The code is contributed by Nidhi goel.


C#




using System;
 
class Program {
    static void Main(string[] args)
    {
        int num = 5;
        if (Parity(num))
            Console.WriteLine(num + " is Odious Number");
        else
            Console.WriteLine(num + " is Evil Number");
    }
 
    static bool Parity(int num)
    {
        int count = 0;
        while (num > 0) {
            count++;
            num &= num - 1;
        }
        return (count % 2 == 1);
    }
}
// This code is contributed by user_dtewbxkn77n


Javascript




function isOdiousNumber(num) {
  return (num.toString(2).split('1').length - 1) % 2 !== 0;
}
 
let num = 5;
if (isOdiousNumber(num)) {
  console.log(num + " is Odious Number");
} else {
  console.log(num + " is Evil Number");
}
 
// This code is contributed by shivregkec


Output

5 is Evil Number

Time Complexity: O(log2n)
Auxiliary Space: O(1)

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads