Thursday 17 August 2017

EVIL NUMBER

EVIL NUMBER:
       In number theory, an evil number is a non-negative number that has an even number of 1s in its binary expansion.
The first evil numbers are:
0, 3, 5, 6, 9, 10, 12, 15, 17, 18, 20, 23, 24, 27, 29, 30, 33, 34, 36, 39 .
These numbers give the positions of the zero values in the Thue–Morse sequence.
Numbers that are not evil are called odious numbers.


/* program to check given number is evil number or not*/


import java.io.*;

public class EvilNumber
{
    public static void main(String args[])throws IOException
    {
        int num, a, count , c, i;
        int binNum[] = new int[20];
        InputStreamReader in = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(in);
        System.out.println("Enter a number:");
        num = Integer.parseInt(br.readLine());
        count = 0;  // count the number of 1's
        c = 0;      // for index of array
        while(num!=0)
        {
            a = num % 2;
            binNum[c] = a;
            if(a == 1)
             count++;
            num = num / 2;
            c++;
        }
        System.out.println("Total number of 1 is = " + count);
        // Print the binary number
        for(i=c-1; i>=0 ; i--)
        {
            System.out.print(binNum[i]+" ");
        }
        // Checking the number is Evil or Not
        if(count % 2 == 0)
        {
            System.out.println("\nEntered number is Evil an Number");
        }
        else
        {
            System.out.println("\nEntered number is not an Evil Number");
        }
    }   
}



OUTPUT:


Enter a number:
10
Total number of 1 is = 2
1 0 1 0 
Entered number is Evil an Number



Enter a number:
13
Total number of 1 is = 3
1 1 0 1 
Entered number is not an Evil Number


No comments:

Post a Comment