AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



corejava-programming: Evil Number

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 an even number of 1's.

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

Design a program to accept a positive whole number and find the binary equivalent of the number and count the number of 1's in it and display whether it is an Evil number or not with an appropriate message. Output the result in format given below :

Input specification:
The first line contains the number ´n´

Output specification:

The first line will contain the binary equivalent

The second line will contain the number of 1´s in the binary equivalent

If the number of 1´s in the second line is even then the third line will print n is an evil number´ else it will print n is not an evil number

Sample Input 1:

15

Sample Output 1:

1111

4

15 is an evil number

Sample Input 2:

26

Sample Output 2:

11010

3

26 is not an evil number

corejava x 351
programming x 167
Posted On : 2017-03-10 22:31:41.0
profile Raksha - anyforum.in Raksha
2510
up-rate
4
down-rate

Answers


import java.util.Scanner;

public class EvilNumber {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int number=input.nextInt();
String binaryString=Integer.toBinaryString(number);
System.out.println(binaryString);
int countOfOnes=binaryString.length() - binaryString.replaceAll("1", "").length();
System.out.println(countOfOnes);
System.out.println(number +" is "+(countOfOnes%2==0?"":"not ")+"an evil number");
}
}

Posted On : 2017-03-21 20:15:17
Satisfied : 1 Yes  0 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523187021168
Reply This Thread
up-rate
5
down-rate

import java.util.Scanner;
class BinaryEvil
{
public static void main(String args[])
{
Scanner s= new Scanner(System.in);
String D=s.next();
int B=Integer.parseInt(Integer.toBinaryString(Integer.parseInt(D)));
System.out.println(B);
int sum=0;
while (B>9)
{
sum=0;
while (B > 0)
{
int rem;
rem = B % 10;
sum = sum + rem;
B = B / 10;
}
B = sum;
}
System.out.println(sum);
if(sum%2==0)
{
System.out.println(D+"is an Evil number");
}
else
{
System.out.println(D+"is not an Evil number");
}
}
}

Posted On : 2017-03-22 00:57:58
Satisfied : 0 Yes  0 No
profile Sudheer Kumar Javvadi - anyforum.in Sudheer Kumar Javvadi
0960
Reply This Thread
up-rate
0
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in