Evil Number

 //An Evil number is a positive whole number which has even number of 1's in its binary equivalent.

import java.util.*;

class evil

{//class evil opens

    public static void main(String[]args)

    {//main method opens

        Scanner sc=new Scanner(System.in);

        System.out.println("Enter a no");

        int n=sc.nextInt();//variable n to accept the no

        int r=0,c=0;//variable r to extract the digits,variable c to count the no of 1s present in the binary notation

        while(n>0)//10>0t//5>0t//2>0t//1>0t//0>0false

        {

            r=n%2;//0//1//0//1

            if(r==1)//f//t//f//t

            c=c+1;//0//1//2

            n=n/2;//10/2=5//5/2=2//2/2=1//1/2=0

        }

        if(c%2==0)//2%2==0 true

        System.out.println("It is an evil no");

        else

        System.out.println("It is not an evil no");

    }//main method closes

}//class closes


No comments:

Post a Comment

any problem in any program comment:-

Second largest number in java using ternary operator

 //Largest second number using ternary operator public class Main { public static void main(String[] args) { int a=5,b=6,c=7; int ...