Saturday, July 3, 2021

Evil Number Program Using BlueJ

 

An Evil number is a positive whole number which has even number of 1’s in its binary equivalent.
Example: Binary equivalent of 9 is 1001, which contains even number of 1’s.
 Thus, 9 is an Evil Number.

A few Evil numbers are 3, 5, 6, 9....

Design a program to accept a positive whole number ‘N’ where N>2 and N<100. Find the binary
equivalent of the number and count the number of 1s in it and display whether it is an Evil number or
not with an appropriate message.

Test your program with the following data and some random data:

Example 1:
INPUT: N = 15
BINARY EQUIVALENT: 1111
NUMBER OF 1’s: 4
OUTPUT: EVIL NUMBER

Example 2:
INPUT: N = 26
BINARY EQUIVALENT: 11010
NUMBER OF 1’s: 3
OUTPUT: NOT AN EVIL NUMBER

Example 3:
INPUT: N = 145
OUTPUT: NUMBER OUT OF RANGE

import java.util.*;
class EvilNumber
{
    void take(int n)
    {       
        String s="";
        String s1="";
        int c=0,t=0;
        while(n>0)
        {
            int r=n%2;
            s=r+s;
            n=n/2;
        }
        t=Integer.parseInt(s);
        while(t!=0)
        {
            int r=t%10;
            if(r==1)
            c++;
            t=t/10;
        }
        if(c%2==0)
        {
            System.out.println("The original number is "+n);
            System.out.println("Its binary equivalent is "+s);
            System.out.println("It is an Evil Number");
        }
        else
        {
            System.out.println("The original number is "+n);
            System.out.println("Its binary equivalent is "+s);
            System.out.println("It is not a Evil Number");
        }
    }
public static void main(String args[])
{
 EvilNumber obj = new EvilNumber();
obj.take(23);
}
}

Other Programs On Numbers: CLICK HERE

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner