# Java Program to Check Evil Number

In the previous article, we have discussed Java Program to Check Sphenic Number

In this article we are going to understand what Evil number is and how we can check whether a number is Evil number or not in Java with examples.

## Program to Check Evil Number

Evil numbers are numbers having even number of ones in their binary equivalent. The numbers that don’t come under this category are called odious number.

 Example :

15:  Binary = 1111, Evil number
9:    Binary = 1001, Evil number
62:  Binary = 111110, Odious number

In the above examples the numbers 15 and 9 are Evil numbers as their binary equivalents have even number of 1’s in them. However 62 is not a Evil number, it is a Odious number as it has odd number of 1’s.

Let’s see different ways to do it.

Approach :

1. Enter/declare a number and store it.
2. We convert it into its binary using toBinaryString() function and store it.
3. We check whether the binary equivalent has the same even number of 1’s in it.
4. If the binary has even number of 1’s, then the number is said to be a Evil number, else it is a tedious number.

### Method-1: Java Program to Check Evil Number By Using Static Input

import java.util.*;
public class EvilNumber{
public static void main(String args[])
{
//A number declared;
int num = 15;

if(checkNum(num))
{
System.out.println(num+" is an Evil number");
}
else
{
System.out.println(num+" is Not an Evil number");
}
}

// Function to convert decimal to binary
static long convertToBinary(int num)
{
long bin = 0;
int remainder = 0, j=1;
while(num!=0)
{
remainder = num%2;
bin += remainder * j;
num/=2;
j *= 10;
}

return bin;
}

// Function to check whether the number is evil or not
static boolean checkNum(int num)
{
// Using the library function to convert
long bin = convertToBinary(num);
int count = 0;

while(bin!=0)
{
if(bin%10==1)
count++;

bin = bin/10;
}

if(count%2 == 0)
return true;
return false;
}
}

### Method-2: Java Program to Check Evil Number By Using User Input

import java.util.*;
public class EvilNumber{
public static void main(String args[])
{
//Taking the number as input from the user using scanner class
Scanner scan = new Scanner(System.in);
System.out.print("Enter a number : ");
int num = scan.nextInt();

if(checkNum(num))
{
System.out.println(num+" is an Evil number");
}
else
{
System.out.println(num+" is Not an Evil number");
}
}

// Function to convert decimal to binary
static long convertToBinary(int num)
{
long bin = 0;
int remainder = 0, j=1;
while(num!=0)
{
remainder = num%2;
bin += remainder * j;
num/=2;
j *= 10;
}

return bin;
}

// Function to check whether the number is evil or not
static boolean checkNum(int num)
{
// Using the library function to convert
long bin = convertToBinary(num);
int count = 0;

while(bin!=0)
{
if(bin%10==1)
count++;

bin = bin/10;
}

if(count%2 == 0)
return true;
return false;
}
}

Output:

Case-1

Enter a number : 15
15 is an Evil number

Case-2

Enter a number : 62
62 is Not an Evil number

### By C++ Language

#include <iostream>
using namespace std;

int checkNumber(int num) {

int count = 0;
while (num != 0) {
int r = num % 2;
if(r == 1)
count++;
num = num / 2;
}

if (count % 2 == 0)
return 1;
else
return 0;
}

int main(void)
{
int num = 15;
if (checkNumber(num) )
cout<<"Evil Number";
else
cout<<"Not Evil Number";
return 0;
}
Output:

Evil Number

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Related Java Programs: