Evil Number in Java2 min read

In this section, we will learn about the evil number and the java program on evil numbers. We will write java code to check the given number for evil number.

What is Evil Number?

A number is said to be an evil number if it is a positive whole number that has an even number of 1’s in its binary expansion. Binary expansion refers to the binary representation of a number that is with 0(zero) and 1(one).

Odious numbers are opposite to evil number.

Example of evil number:

Input = 3
The binary value of 3 = 11. Since the 1’s are even, therefore 3 is an evil number.

Input = 23
The binary value of 23 = 10111. Since the 1’s are even, therefore 23 is an evil number.

Input = 4
The binary value of 4 = 100. Since the 1’s are odd, therefore 23 is not an evil number. But it is an odious number.

Now we know what is Evil number, let us see Evil Number Program in Java with source code.


Java program to check whether the given number is an evil number or not.

We will take a number and find its binary equivalent and store the binary value in a variable. Then we will check the number of 1’s present in that binary variable. If the number of 1’s is even then it is an evil number else the given number is not an evil number.

Output: Evil Number Java.

In the above java evil program, we created two user-defined functions, one for the conversion of an integer to binary and another one that checks the number of 1’s present in the binary number and returns the true-false value to the main function accordingly.


MORE

Java Program to check Krishnamurthy Number

In this tutorial, we will learn about Krishnamurthy numbers and write a Krishnamurthy Number program in Java. We will write two programs for Krishnamurthy number …
Read More

Java Program for ISBN Number

In this tutorial, we will learn about the ISBN (International Standard Book Number) and write a program to check for the ISBN Number in Java …
Read More

C++ Memory Management

Memory management refers to the process of managing the computer memory while assigning the space to the program’s variable to improve the overall performance. Requirement …
Read More

C++ Pointer to Pointer

As we know by now that a pointer stores the address of the pointed variable. But it is not the only use, pointer also stores …
Read More

C++ Passing Pointers to Functions

A function is a user-defined block of codes that executes some specific task assigned to it invoked by its name. If there is an argument …
Read More

C++ Null Pointer

We use a null pointer when we do not have the exact address to assign to a pointer. It is considered a good practice and …
Read More