
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How To Check Whether a Number is a Evil Number or Not in Java?
A number is said to be an Evil number, if the number of 1’s present in the binary conversion of the given number is even.
For more clarification, we have to first convert the given number into binary number. After converting we have to calculate how many ones are present. If the numbers of 1’s is even times then we can say the given number is an Evil number. If the numbers of 1’s is odd times then we can say the given number is an Odious number.
In this article we will see how to check if a number is evil by using Java programming language.
To show you some instances
Instance-1
Input number is 20
Let’s check it by using the logic of Evil number
The binary of 20 is = 10100
Numbers of one’s present= 2.
As we notice here we got an even number
Hence, 20 is an Evil number
Instance-2
Input number is 55.
Let’s check it by using the logic of Evil number
The binary of 55 is = 110111
Numbers of one’s present= 5.
As we notice here we got an odd number.
Hence, 55 is not an Evil number.
Instance-3
Input number is 112.
Let’s check it by using the logic of Evil number.
The binary of 112 is = 1110000
Numbers of one’s present= 3.
As we notice here we got an odd number.
Hence, 112 is not an Evil number.
Some other examples of evil numbers include 0, 3, 5, 6, 9, 10, 12, 15, 17, 18, 20, 23, 24, 27, 29, ..., and so on.
Step 1 − Get an integer number either by initialization or by user input.
Step 2 − First convert the input number to binary number and store it into a variable.
Step 3 − Then take a loop till the binary number is equal to zero.
Step 4 − In every iteration, we are checking whether the unit place in 1 or not and after counting we are removing the unit place number as well.
Step 5 − Finally, whatever the count value we got, check whether that number is an even number or odd.
Step 6 − If even then we conclude that the number is an evil number otherwise the number is not an evil number.
Multiple Approaches
We have provided the solution in different approaches
By Using Static Input Value
By Using User Defined Method
Let’s see the program along with its output one by one.
Approach-1: By Using Static Input Value
In this approach one integer value will be initialized in the program and then by using the algorithm we can check whether a number is a an evil number or not.
Example
import java.util.*; import java.io.*; public class Main{ //main method public static void main(String[] args){ //declare a variable whcih store the static input int inputNumber = 15; //declare other variables for calculations long binaryOfInputNumber = 0; int temp= inputNumber; int reminder = 0; int i = 1; //binary conversion while(temp != 0){ //find the unit place number reminder = temp % 2; binaryOfInputNumber += reminder * i; temp = temp / 2; i = i * 10; } // for counting the number of 1's we declare a variable int count = 0; //take a loop with a condition till the value is zero while(binaryOfInputNumber != 0){ // check whether the unit place consisting 1 or not if(binaryOfInputNumber % 10 == 1) //increment the count value count++; // after counting removing the unit place in each iteration binaryOfInputNumber = binaryOfInputNumber / 10; } // conditon for checking the count value is even or odd // If even then the number is an evil number otherwise not if(count % 2 == 0) //return true if satisfied System.out.println(inputNumber + " is an evil number"); //return false if not satisfied else System.out.println(inputNumber + " is not an evil number"); } }
Output
15 is an evil number
Approach-2: By Using User Defined
In this approach, the user will be asked to enter the input number and pass this number as parameter in a user defined method then inside the method by using the algorithm we can check whether the number is an evil number or not.
Example
public class Main { //main method public static void main(String[] args){ //initialize input value int num = 56; System.out.println("Enter a number : "+num); //pass the input value into our user defined method //if its return true then it's an evil number if(checkEvil(num)) System.out.println(num + " is an evil number"); else System.out.println(num + " is not an evil number"); } //define the user defined method public static boolean checkEvil(int inputNumber){ //convert number to binary //the method return a string value String str = Integer.toBinaryString(inputNumber); //type casting string to Integer //store it into a variable long binaryOfInputNumber= Long.parseLong(str); // for counting the number of 1's we declare a variable int count = 0; //binary conversion while(binaryOfInputNumber != 0){ // check whether the unit place consisting 1 or not if(binaryOfInputNumber % 10 == 1) //increment the count value count++; // after counting removing the unit place in each iteration binaryOfInputNumber = binaryOfInputNumber / 10; } // conditon for checking the count value is even or odd if(count % 2 == 0) //return true if satisfied return true; //return false if not satisfied return false; } }
Output
Enter a number : 56 56 is not an evil number
In this article, we explored how to check a number whether it is an evil number or not in Java by using three different approaches.
- Related Articles
- How to Check Whether a Number is Krishnamurthy Number or Not in Java?
- How To Check Whether a Number is Strontio Number or Not in Java?
- How To Check Whether a Number is Tcefrep Number or Not in Java?
- How To Check Whether a Number is Tech Number or Not in Java?
- How To Check Whether a Number Is a Pronic Number or Not in Java?
- How To Check Whether a Number Is a Bouncy Number or Not in Java?
- How To Check Whether a Number Is a Fascinating Number or Not in Java?
- How To Check Whether a Number is a Harshad Number or Not in Java?
- How To Check Whether a Number Is a Insolite Number or Not in Java?
- How To Check Whether a Number is a Keith Number or Not in Java?
- How To Check Whether a Number Is a Niven Number or Not in Java?
- How To Check Whether a Number is a Sunny Number or Not in Java?
- How to Check Whether a Number is a Triangular Number or Not in Java?
- How To Check Whether a Number is a Unique Number or Not in Java?
- How To Check Whether a Number is a Goldbach Number or Not in Java?
