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 numbers.

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 is, 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.

//Run 1
Enter the positive number: 23
23 is an evil number

//Run 2
Enter the positive number: 4
4 is not an evil number

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

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …
Read More

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More