Ad Home

Theme images by kelvinjay. Powered by Blogger.


Header Ads





General QA Concept


» » How to calculate an Evil No in java :: How i did it

Evil No-Definition

A number N will be evil number if it has an even number of 1's in it's binary expansion.


1. Get the number from user.
2. Find binary form of the number
3. The binary stream will be an String of 1s and 0s.
4. Now create a counter and initialize it to 0(to satisfy the input for 0. binary of decimal 0 is also zero).
5. Convert the String to array.
6. Check the array element one by one for 1.
7. if 1 found increment the counter by one
8. check the number of 1s are even or odd.
9. If it is found are done that is the Evil number.

Lets code

1. Get the number from user.   check here
2. Find binary form of the number...check here
3. The rest of the code looks like-

    public void checkEvil(String input)
//declare the counter and initialize it to 0
     int counter=0;
    int len=input.length();
//Scan through the binary stream for 1
     for(int i=0;i<len;i++)
     if (input.charAt(i)=='1')
 //if an one found increment counter by 1
// Check the odd even logic
     if((counter%2)==1) {
         System.out.println("not evil");

Newer Post
Older Post

No comments:

Leave a Reply