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>
Output32 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
Time Complexity: O(log2n)
Auxiliary Space: O(1)
Similar Reads
Ugly Number ll An ugly number is a positive integer whose only prime factors are 2, 3, and 5, with 1 included by convention. The sequence begins as 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, and so on. Given an integer n, return the nth ugly number.Examples: Input: n = 5Output: 5Explanation: Ugly Numbers - 1, 2, 3, 4, 5,
15+ min read
Anti-perfect Number Given a number N, the task is to check if N is an Anti-perfectNumber or not. If N is an Anti-perfectNumber then print "Yes" else print "No". An anti-perfect Number is a number that is equal to the sum of the reverse of its proper divisors. Examples: Input: N = 244 Output: Yes Explanation: proper div
7 min read
Odious number Odious number is a nonnegative number that has an odd number of 1s in its binary expansion. The first few odious numbers are therefore 1, 2, 4, 7, 8, 11, 13, 14, 16, 19... Given a number check if its a odious number or not. Examples : Input : 16 Output : Odious Number Explanation: Binary expansion o
4 min read
Check if a number is Bleak A number 'n' is called Bleak if it cannot be represented as sum of a positive number x and set bit count in x, i.e., x + countSetBits(x) is not equal to n for any non-negative number x.Examples : Input : n = 3 Output : false 3 is not Bleak as it can be represented as 2 + countSetBits(2). Input : n =
12 min read
Vampire Number Introduction to Vampire Number and its implementation using python. Introduction In mathematics, a vampire number (or true vampire number) is a composite natural number v, with an even number of digits n, that can be factored into two integers x and y each with n/2 digits and not both with trailing
10 min read
Reversible numbers A number is said to be a reversible if sum of the number and its reverse had only odd digits. The problem is to find out if a number is reversible or not. Examples: Input: 36 Output: Reversible number as 36 + 63 = 99 has only odd digits. Input: 409 Output: Reversible number as 409 + 904 = 1313 has o
11 min read