Check whether number is Evil Number or Not

An Evil number is a non-negative number that has an even number of 1s in its binary expression.

Example:

               Given Number=3
               30011(Here Even number of 1s present)
               so, 3 is an Evil Number.
                 
                Given Number=4
               40100(Here Even number of 1s, not present)
               so, 120 is not an Evil Number.
                                          
Problem statement:-  Program to check whether the number is Evil Number or Not.

Data requirement:-

   Input Data:- num

   Output Data:- String output

   Additional Data:- num, one_c

Program in C

Here is the source code of the C Program to check whether the number is Evil Number or Not.

Code:

//Evil Number Or Not
#include<stdio.h>
int main()
{
    int num;
    printf("Enter the number:");
    scanf("%d",&num);
   int one_c=0;
   while(num!=0)
   {
       if(num%2==1)
       {
           one_c++;
       }
           num/=2;

   }
   if(one_c%2==0)
    printf("It is an Evil Number.");
   else
    printf("It is not an Evil Number.");
}

Input/Output:
Enter the number:3
It is an Evil Number.   

Program in C++

Here is the source code of the C++ Program to check whether the number is Evil Number or Not.

Code:

#include <iostream>
using namespace std;
int main()
{
    int num;
    cout<<"Enter the number:";
    cin>>num;
   int one_c=0;
   while(num!=0)
   {
       if(num%2==1)
       {
           one_c++;
       }
           num/=2;

   }
   if(one_c%2==0)
    cout<<"It is an Evil Number.";
   else
    cout<<"It is not an Evil Number.";
}

Input/Output:
Enter the number:4
It is not an Evil Number.

Program in Java
  
Here is the source code of the Java Program to check whether the number is Evil Number or Not.

Code:

import java.util.Scanner;
public class EvilNumberOrNot {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
     int num;
     System.out.println("Enter a number:");
     num=cs.nextInt();
     int one_c=0;
     while(num!=0)
     {
         if(num%2==1)
         {
             one_c++;
         }
             num/=2;

     }
     if(one_c%2==0)
        System.out.println("It is an Evil Number.");
    else
         System.out.println("It is not an Evil Number.");
  cs.close();
}
}

Input/Output:
Enter a number:
15
It is an Evil Number.

Program in Python
  
Here is the source code of the Program to check whether the number is Evil Number or Not.

Code:

num=int(input("Enter a number:"))
one_c=0
while num!=0:
    if num%2==1:
        one_c+=1
    num//=2
if one_c%2==0:
    print("It is an Evil Number.")
else:
   print("It is Not an Evil Number.")

Input/Output:
Enter a number:50
It is Not an Evil Number.

More:-

C/C++/Java/Python Practice Question 

Post a Comment

0 Comments