Java Program to Check Evil Number

You are currently viewing Java Program to Check Evil Number

Java program to check Evil number

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(;
        // 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.


Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply