Skip to content

Commit 65bc419

Browse files
author
codehouseindia
authored
Merge pull request codehouseindia#147 from aditya-gite-04/master
EVIL number in JAVA
2 parents 15d0dc8 + 9fb7cde commit 65bc419

1 file changed

Lines changed: 54 additions & 0 deletions

File tree

EVIL number

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import java.util.*;
2+
class EvilNumber
3+
{
4+
String toBinary(int n) // Function to convert a number to Binary
5+
{
6+
int r;
7+
String s=""; //variable for storing the result
8+
9+
char dig[]={'0','1'}; //array storing the digits (as characters) in a binary number system
10+
11+
while(n>0)
12+
{
13+
r=n%2; //finding remainder by dividing the number by 2
14+
s=dig[r]+s; //adding the remainder to the result and reversing at the same time
15+
n=n/2;
16+
}
17+
return s;
18+
}
19+
20+
int countOne(String s) // Function to count no of 1's in binary number
21+
{
22+
int c = 0, l = s.length();
23+
char ch;
24+
for(int i=0; i<l; i++)
25+
{
26+
ch=s.charAt(i);
27+
if(ch=='1')
28+
{
29+
c++;
30+
}
31+
}
32+
return c;
33+
}
34+
35+
public static void main(String args[])
36+
{
37+
EvilNumber ob = new EvilNumber();
38+
Scanner sc = new Scanner(System.in);
39+
40+
System.out.print("Enter a positive number : ");
41+
int n = sc.nextInt();
42+
43+
String bin = ob.toBinary(n);
44+
System.out.println("Binary Equivalent = "+bin);
45+
46+
int x = ob.countOne(bin);
47+
System.out.println("Number of Ones = "+x);
48+
49+
if(x%2==0)
50+
System.out.println(n+" is an Evil Number.");
51+
else
52+
System.out.println(n+" is Not an Evil Number.");
53+
}
54+
}

0 commit comments

Comments
 (0)