Skip to main content

Evil Number

AJ Java Programs

Program 1:

Write a Java program to check whether the inputted number is an Evil Number or not.

A Number is said to be an Evil Number when Binary Equivalent of the number contains Even Number of  1's
For Example : 5 Binary equivalent 101 Thus It have  Even Number of 1s



Syntax:

/*
 *  100 % working code.
 */
import java.io.*;
class evilnum
{
    public static void main(String args[])throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int n,c,d=0,sum=0,x=0,a=0,b=0;
        System.out.println("ENTER A DECIMAL NUMBER");
        n=Integer.parseInt(br.readLine());
        while(n!=0)
        {
            c=0;
            d=n%2;
            sum+=d*Math.pow(10,c++);
            n=n/2;
        }
            while(sum!=0)
            {
                x=sum%10;
                if(x==1)
                a++;
                else b++;
                sum=sum/10;
            }
            if(a%2==0)
            System.out.println("THE NUMBER IS EVIL NUMBER");
            else
            System.out.println("NO THE NUMBER IS NOT EVIL NUMBER");
        }

    }

Here
n is to Input Number
c is to obtain required power
sum is to obtain Binary Equivalent
a is  to Find number of ones





Comments

Popular posts from this blog

Disarium Number

AJ Java Programs Program 4: Disarium Number A number will be called DISARIUM if sum of its digits powered with their respective position is equal to the original number. For example  135 is a DISARIUM (Workings 1 1 +3 2 +5 3  = 135, some other  DISARIUM  are 89, 175, 518 etc) Syntax: import java.io.*; class disarium {     public static void main(String args[])throws IOException     {         BufferedReader br=new BufferedReader(new InputStreamReader(System.in));         int n,n1,n2,c=0,sum=0,d=0;         System.out.println("Enter a Num");         n=Integer.parseInt(br.readLine());         n2=n1=n;         while(n2!=0)         {             n2=n2/10;             c++;         }       ...

Arranging according to potential.

PROGRAM: Requested by Sir  Ankur Acharya   ARRANGING WORDS IN A STRING ACCORDING TO THEIR POTENTIAL The encryption of alphabets are to be done as follows: A = 1 B = 2 C = 3 . . . Z = 26 The potential of a word is found by adding the encrypted value of the alphabets. Example: KITE Potential = 11 + 9 + 20 + 5 = 45 Accept a sentence which is terminated by either “ . ” , “ ? ” or “ ! ”. Each word of sentence is separated by single space. Decode the words according to their potential and arrange them in ascending order. Output the result in format given below: Example 1 INPUT : THE SKY IS THE LIMIT. POTENTIAL : THE = 33 SKY = 55 IS = 28 THE = 33 LIMIT = 63 OUTPUT : IS THE THE SKY LIMIT Example 2 INPUT : LOOK BEFORE YOU LEAP. POTENTIAL : LOOK = 53 BEFORE = 51 YOU = 61 LEAP = 34 OUTPUT : LEAP BEFORE LOOK YOU SYNTAX import java.io.*; class Potential {     public static void main(String args[])throws IOException     {       ...

MAGICAL/ MAGIC NUMBER USING RECURSION

PROGRAM :  MAGICAL NUMBER BY RECURSION This  program  checks if a  number  is a  Magic number  in  JAVA . A  number  is said to be a  Magic number  if the sum of its digits are calculated till a single digit is obtained by recursively adding the sum of its digits. If the single digit comes to be 1 then the  number  is a  magic number . Example: 10999 => 1+0+9+9+9=28=>2+8=>10=>1+0= 1                  199    =>1+9+9 =>19=> 1+9=>10=>1+0= 1 SYNTAX import java.io.*; class magic  {    static int c;     public static void main(String arsg[])throws IOException     {         BufferedReader br=new BufferedReader(new InputStreamReader(System.in));         int n;int d=0;int m;         System.out.println("ENter a NUmber");   ...