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.


C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a number using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

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 …

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 …