Java program for Evil Number

Java program for Evil Number

Java program for Evil Number


A positive number is an evil number that has even numbers of 1’s.
For example, 89 is an evil number as it has four 1’s and its binary equivalence is 
1 0 1 1 0 0 1
Few evils number are 3, 5, 6, 9, 10, 12, 15, 17, 18, 20

import java.io.*;
/**
 * class EvilNumberProgram.
 * @author ( Md Anwar)
 */
public class EvilNumberProgram
{
    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 to check Evil 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 to check Evil number :
5
Total number of 1 is = 2
1 0 1
Entered number is Evil an Number

Enter a number to check Evil number :
9
Total number of 1 is = 2
1 0 0 1
Entered number is Evil an Number

Enter a number to check Evil number :
89
Total number of 1 is = 4
1 0 1 1 0 0 1
Entered number is Evil an Number

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

khurshid
Variable Description
Sl. No.
Variable Name
Data Type
Purpose
1
main()
static void
Main function
2
num
int
To store the number
3
a
int
For taking reminder of num % 2
4
count
int
To take total of 1’s
5
i
int
For running loop
6
c
int
Index of binNum[]
7
binNum[]
int
Array of binary number
8
in
Object
Object of class InputStreamReader
9
br
Object
Object of class BufferedReader
10
ch
char
To extract a single character inside the loop from the sentence

Some more programs

SHARE THIS
Previous Post
Next Post