Wednesday, August 5, 2020

Write a program to input a number and check whether it is Evil Number or not? (5...binary...101.no. of ones is 2 and it is even.so it is evil no.)

76)  Write a program to input a number and check whether it is Evil Number or not? (5...binary...101.no. of ones is 2 and it is even.so it is evil no.)


import java.util.*;
public class Evil_num
{
    public static void main(String args[ ])
   {
           Scanner sc = new Scanner (System.in);
           System.out.println("Enter a number");
           int b = sc.nextInt();
           int a = b;
           int r = 0;
           String c = "";
           while(a>=1)
           {
               if(a%2==0)
               {
                   c=c+"0";
               }
               else
               {
                   c=c+"1";
               }
               a=a/2;
           }
           int k = c.length();
           for(int i = k-1;i>=0;i--)
           {
               if(c.charAt(i)=='1')
               {
                   r++;
               }
           }
           if(r%2==0)
           {
               System.out.println("Entered number is a EVIL Number");
           }
           else
           {
               System.out.println("Entered number is not a EVIL Number");
           }
   }
}

Output : 




No comments:

Post a Comment

if you have any doubts, Please let me know.
Your Truly
Shashank Porwal

 jhvghvgk ;nkjn ,nkjhj