An Evil Number is a number which contains even number of 1’s in its binary
equivalent.
Example: Binary equivalent of 10 = 1010 which contains even number on 1’s.
Thus, 10 is an Evil Number.
Design a class Evil to check if a given number is an Evil number or not. Some of the
members of the class are given below: .
Class name : Evil
Data members/instance variables:
nnum : to store a positive integer number
bin : to store the binary equivalent
Methods / Member functions:
Evil( ) : default constructor to initialize the data member
with legal initial value
void acceptNum( ) : to accept a positive integer number
void rec_bin (int x) : to convert the decimal number into its binary
equivalent using recursive technique
void check( ) : to check whether   the given number is an Evil
number by invoking the function rec_bin() and
to display the result with an appropriate
message
Specify the class Evil giving details of the constructor( ), void acceptNum( ),
void rec_bin(int) and void check( ). Define a main( ) function to create an object and call
all the functions accordingly to enable the task.
        
Solution:
,,
 


  
import java.util.*;
class Evil
{
  int num;
  String bin;
  public Evil()
  {
      num=0;
      bin="";
  }

  public void acceptnum()
  {
      Scanner sc=new Scanner(System.in);
      System.out.println("Enter the positive number");
      num=sc.nextInt();
  }

  void rec_bin(int x)
  {
      if(x!=0)
      {
          bin=bin+(x%2);
          rec_bin(x/2);            
      }

  }

  void check()
  {   int count=0;
      rec_bin(num);
      for(int i=0;i< bin.length();i++)
      {
          char ch=bin.charAt(i);
          if (ch=='1')
          {
              
              count+=1;
          }
      }
      if (count %2==0)
      {
          System.out.println("Number is Evil");
      }
      else{
          System.out.println("Number is not Evil");
      }
  }

  public static void main(String []args)
  {
      Evil ob=new Evil();
      ob.acceptnum(); 
      ob.check();
  } }                                          
      
         
                                                                  


                                            



Contact Us

REACH US

SERVICES

  • CODING
  • ON-LINE PREPARATION
  • JAVA & PYTHON

ADDRESS

B-54, Krishna Bhawan, Parag Narain Road, Near Butler Palace Colony Lucknow
Contact:+ 919839520987
Email:info@alexsir.com