Javatpoint Logo
Javatpoint Logo

Evil Number Java

The Evil number is another special positive whole number in Java that has an even number of 1's in its binary equivalent. Unlike Prime and Armstrong numbers, Evil number is not so popular and asked by the interviewers.

The numbers which are not evil are called odious numbers. Let's some examples of evil and odious numbers.

  1. 15 is an evil number because in its binary equivalent, i.e., 1111, it has an even number of ones.
  2. 16 is an odious number because in its binary equivalent, i.e., 10000 has not even number of ones
  3. 23 is also an evil number because it has an even number of ones in its binary equivalent, i.e., 10111.

In order to check whether the number is evil or not, we have to follow the following steps:

  1. We first take a number.
  2. We then find the binary equivalent of this number and store it into another variable.
  3. We find the total number of ones in the binary number.
  4. If we found an even number of ones in the binary equivalent number, then the number is an evil number. Else the given number is not an evil number.

Note: To convert a decimal number into binary, we can use the toBinaryString() in-built method or do it manually by using the loop.

Let's implement the code to check whether the number is evil or not.

EvilNumberExample.java

Output

Evil Number Java

Let's implement one more program to get all the Evil numbers in a given range

FindAllEvilNumber.java

Output

Evil Number Java
Next TopicISBN Number Java





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA