Java Program To Check For Evil Number

Chapter: Math Class Last Updated: 28-05-2016 12:48:06 UTC

Program:

            /* ............... START ............... */
                
import java.util.*;

public class JavaEvilNumber {

	String toBinary(int number) // Function to convert a number to Binary
	{
		int r;
		String s = "";

		char dig[] = { '0', '1' }; // array storing the digits (as characters)
									// in a binary number system

		while (number > 0) {
			r = number % 2; // finding remainder by dividing the number by 2
			s = dig[r] + s; // adding the remainder to the result and reversing
							// at the same time
			number = number / 2;
		}
		return s;
	}

	int countOne(String s) // Function to count no of 1's in binary number
	{
		int c = 0, l = s.length();
		char ch;
		for (int i = 0; i < l; i++) {
			ch = s.charAt(i);
			if (ch == '1') {
				c++;
			}
		}
		return c;
	}

	public static void main(String args[]) {
		JavaEvilNumber ob = new JavaEvilNumber();
		Scanner scanner = new Scanner(System.in);

		System.out.print("Enter a positive number : ");
		int number = scanner.nextInt();

		String bin = ob.toBinary(number);
		System.out.println("Binary Equivalent = " + bin);

		int x = ob.countOne(bin);
		System.out.println("Number of Ones = " + x);

		if (x % 2 == 0)
			System.out.println(number + " is an Evil Number.");
		else
			System.out.println(number + " is Not an Evil Number.");
	}
}
                /* ............... END ............... */
        

Output

Enter a positive number : 26
Binary Equivalent = 11010
Number of Ones = 3
26 is Not an Evil Number.
 
Enter a positive number : 420
Binary Equivalent = 110100100
Number of Ones = 4
420 is an Evil Number.
 
Enter a positive number : 659
Binary Equivalent = 1010010011
Number of Ones = 5
659 is Not an Evil Number.

Notes:

  • An Evil number is a positive whole number which has even number of 1’s in its binary equivalent.

Tags

Java Program To Check For Evil Number,Java, Math

Similar Programs Chapter Last Updated
Java Program To Calculate Standard Deviation Math Class 21-09-2017
Java Program To Display Factors Of Number Math Class 12-09-2017
Java Program To Find Power Of Number Using pow() Function Math Class 29-08-2017
Java Program To Find Quotient And Remainder Math Class 25-08-2017
Java Matrix Multiplication Math Class 18-05-2017
Java Matrix Subtraction Math Class 18-05-2017
Java Matrix Addition Math Class 17-05-2017
LCM And GCD Of Two Numbers In Java Math Class 14-05-2017
Java Cube Root Of Number Math Class 10-03-2017
Java Program To Calculate Area Of Right Triangle Math Class 16-12-2016
Java Program To Find Rectangle Perimeter Math Class 15-06-2016
Java Program To Find Circle Perimeter Math Class 15-06-2016
Math.Floor In Java Example Math Class 14-06-2016
Math.ceil In Java Example Math Class 14-06-2016
Java Program To Find Square Root Of Given Number Math Class 14-06-2016
Random Number Generation In Java Example Math Class 14-06-2016
Quadratic Equation In Java Example Math Class 14-06-2016
Java Program To Displays Powers of 2 Example Math Class 14-06-2016
Java Program To Check For Scalar Matrix Math Class 30-05-2016
Java Program To Find Saddle Point In Square Matrix Math Class 30-05-2016
Decimal To Octal Converstion In Java Math Class 30-05-2016
Decimal To Binary Converstion In Java Math Class 30-05-2016
Java Program To Check For Upper Triangular Matrix Math Class 28-05-2016
Java Program To Check For Pronic Number Math Class 28-05-2016
Java Program To Find Value Of Mobius Function For number Math Class 28-05-2016
Java Program To Check For Lower Triangular Matrix Math Class 28-05-2016
Java Program To Check For Harshad Number Math Class 28-05-2016
Java Program To Check Fascinating Number Math Class 28-05-2016
Java Program To Check Bouncy Number Increasing Decreasing Number Math Class 07-02-2018
Tower Of Hanoi In Java Example Math Class 05-11-2016

1 2