Home » Java programs » Java basic programs

Java program to check Evil number

In this java program, we are going to write code to check whether a given number is an Evil number or not.
Submitted by Chandra Shekhar, on January 18, 2018

Given a number, we have to check that the entered number is Evil Number or not.

A number is said to be an Evil number if and only if it is having an even number of 1’s. The entered number must be a whole number.


Input: 3

3 – Is an Evil prime.

Program to check whether the given number is Evil Number or not in Java

package IncludeHelp;

import java.util.Scanner;

public class CheckEvilNumber 
	// Function to convert a number to Binary
	String toBinary(int n) 
		// declare here.
        int r;
        String s=""; 
        // array to store digits.
        char dig[]={'0','1'}; 
        	// find remainder, add it to digit and reverse the digits.
        return s;
	// Function to count no of 1's in binary number
    int countOne(String s) 
        int c = 0, l = s.length();
        char ch;
        for(int i=0; i<l; i++)
        return c;
    public static void main(String args[])
    	// create object for number and scanner class.
        CheckEvilNumber ob = new CheckEvilNumber();
        Scanner sc = new Scanner(System.in);
        // enter the positive number here.
        System.out.print("Enter a positive number : ");
        int n = sc.nextInt();
        String bin = ob.toBinary(n);
        // here print the binary number of the number.
        System.out.println("Binary Equivalent = "+bin);
        int x = ob.countOne(bin);
        // print the number of 1,s
        System.out.println("Number of Ones = "+x);
        // check for evil or not.
            System.out.println(n+" is an Evil Number.");
            System.out.println(n+" is Not an Evil Number.");


First run:
Enter a positive number : 4520
Binary Equivalent = 1000110101000
Number of Ones = 5
4520 is Not an Evil Number.

Second run:
Enter a positive number : 9
Binary Equivalent = 1001
Number of Ones = 2
9 is an Evil Number.

Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.

Comments and Discussions

We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.