An Evil Number is a non-negative integer that has an even number of 1s in its binary expansion.
Some evil numbers are: 3, 5, 6, 9, 10.
This Program video link is provided at the bottom of this post.
Program :-
import java.io.*; import java.util.*; class Evil { public static void main(String args[]) { Scanner in = new Scanner(System.in); int n,p,a,d=0,i,l; String str="",str1; char ch; System.out.print("Enter Number to Convert it into Binary : "); n= in.nextInt(); p=n; while(p>0) { a=p%2; str=a+str; p=p/2; } str1=str; l=str.length(); for(i=l-1;i>=0;i--) { ch= str.charAt(i); if(ch=='1') { d++; } } if(d%2==0) { System.out.println("Original Number : "+n); System.out.println("Binary Form : "+str1); System.out.println("It is an Evil Number : "+n); } else { System.out.println("Original Number : "+n); System.out.println("Binary Form : "+str1); System.out.println("It is not an Evil Number : "+n); } } // end of main method }; // end of class
Note : The program is written in the easiest way possible so try understand it. If you face any problem in understanding this program than feel free to contact/comment.
All the best :)
Keep Learning :)
Comments