Write a program to check whether the given number is Evil Number or not in Java
A number is said to be an Evil number if and only if the binary equivalent of the number is having an even number of 1’s. The entered number must be a whole number.

import java.io.*;
public class Evil_no
{
     int num;
     String s="";
     int r=0;
    int n;
    void input()throws IOException
    {
       BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
       System.out.println("Enter the number");
       num=Integer.parseInt(br.readLine());
       n=num;
    }
    boolean compute()
    {
        int count=0;
        while(num!=0)
        {
            s+=num%2;
            num=num/2;
        }
        int l=s.length();
        
        for(int i=0;i<l;i++)
        {
            r+=s.charAt(i);
            
        }
        if(r%2==0)
        return true;
        else
        return false;
    }
    void display()
    {
        System.out.println("The number is:"+n);
        System.out.println("Binary equivalent is:"+s);
        if(compute()==true)
        System.out.println("Evil number");
        else
        System.out.println("Not evil number");
    }
Public static void main() throws IOEXCEPTION
{
Evil_no Obj=new Evil_no();
Obj.input();
Obj.display();
}    
}

Output:-

Enter the number

12

The number is 12

Binary equivalent is 1100

Evil number

LEAVE A REPLY

Please enter your comment!
Please enter your name here