Write a program in java to input a number and check whether it is an evil number or not.

Evil Number : Binary equivalent of 9 is 1001, ehich contains even number of 1’s .
Design a program to accept a positive whole number and find the binary  equivalent of the number and count the number of 1’s in it and display whether it is a Evil Number or not with an appropriate message . Output the result in format given below ;

EXAMPLE 1
INPUT : 15
BINARY EQUIVALENT :1111
NO OF 1’S :4
OUTPUT :EVIL NUMBER

Answers:

import java.util.*;
class EvilNumber
{
String toBinary(int n)      // Function to convert a number to Binary
{
int r;
String s="";           //variable for storing the result
char dig[]={'0','1'};    //array storing the digits (as characters) in a binary number system while(n>0)
{
r=n%2;             //finding remainder by dividing the number by 2
s=dig[r]+s;    //adding the remainder to the result and reversing at the same time n=n/2;
}
return s;
}
int countOne(String s)         // Function to count no of 1's in binary number
{
int c = 0, l = s.length(); char ch;
for(int i=0; i<l; i++)
{
ch=s.charAt(i); if(ch=='1')
{
c++;
}
}
return c;
}
public static void main(String args[])
{

EvilNumber ob = new EvilNumber(); Scanner sc = new Scanner(System.in);

System.out.print("Enter a positive number : "); int n = sc.nextInt();

String bin = ob.toBinary(n); System.out.println("Binary Equivalent = "+bin);

}

}

Output: