Skip to main content

Evil Number

AJ Java Programs

Program 1:

Write a Java program to check whether the inputted number is an Evil Number or not.

A Number is said to be an Evil Number when Binary Equivalent of the number contains Even Number of  1's
For Example : 5 Binary equivalent 101 Thus It have  Even Number of 1s



Syntax:

/*
 *  100 % working code.
 */
import java.io.*;
class evilnum
{
    public static void main(String args[])throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int n,c,d=0,sum=0,x=0,a=0,b=0;
        System.out.println("ENTER A DECIMAL NUMBER");
        n=Integer.parseInt(br.readLine());
        while(n!=0)
        {
            c=0;
            d=n%2;
            sum+=d*Math.pow(10,c++);
            n=n/2;
        }
            while(sum!=0)
            {
                x=sum%10;
                if(x==1)
                a++;
                else b++;
                sum=sum/10;
            }
            if(a%2==0)
            System.out.println("THE NUMBER IS EVIL NUMBER");
            else
            System.out.println("NO THE NUMBER IS NOT EVIL NUMBER");
        }

    }

Here
n is to Input Number
c is to obtain required power
sum is to obtain Binary Equivalent
a is  to Find number of ones





Comments