# Find out all Evil numbers present within a given range

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 Find out all Evil numbers present within a given range(1 to n).

Data requirement:-

Input Data:- range1, range2

Output Data:- i, range1, range2

Program in C

Here is the source code of the C Program to Find out all Evil numbers present within a given range.

Code:

//Evil Number in range
#include<stdio.h>
int main()
{
int range1,range2,i;
printf("Enter a range:");
scanf("%d %d",&range1,&range2);
printf("Evil Numbers between %d and %d are: ",range1,range2);
for(i=range1;i<=range2;i++)
{
int one_c=0;
int num=i;
while(num!=0)
{
if(num%2==1)
{
one_c++;
}
num/=2;

}
if(one_c%2==0)
printf("%d ",i);
}
}

Input/Output:
Enter a range:1
50
Evil Numbers between 1 and 50 are: 3 5 6 9 10 12 15 17 18 20 23 24 27 29 30 33 34 36 39 40 43 45 46 48

Program in C++

Here is the source code of the C++ Program to Find out all Evil numbers present within a given range.

Code:

#include <iostream>
using namespace std;
int main()
{
int range1,range2,i,j,flag=0;
cout<<"Enter a range:";
cin>>range1;
cin>>range2;
cout<<"Evil numbers between "<<range1<<" and "<<range2<<" are: ";
for(i=range1;i<=range2;i++)
{
int one_c=0;
int num=i;
while(num!=0)
{
if(num%2==1)
{
one_c++;
}
num/=2;

}
if(one_c%2==0)
cout<<i<<" ";
}
}

Input/Output:
Enter a range:100
125
Evil numbers between 100 and 125 are: 101 102 105 106 108 111 113 114 116 119 120 123 125

Program in Java

Here is the source code of the Java Program to Find out all Evil numbers present within a given range.

Code:

import java.util.Scanner;
public class EvilNumberInRange {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int range1,range2;
System.out.println("Enter a range:");
range1=cs.nextInt();
range2=cs.nextInt();
System.out.println("Evil numbers between "+range1+" and "+range2+" are: ");
for(int i=range1;i<=range2;i++)
{
int one_c=0;
int num=i;
while(num!=0)
{
if(num%2==1)
{
one_c++;
}
num/=2;

}
if(one_c%2==0)
System.out.print(i+" ");
}
cs.close();
}
}

Input/Output:
Enter a range:
600
610
Evil numbers between 600 and 610 are:
600 603 605 606 609 610

Program in Python

Here is the source code of the Program to Find out all Evil numbers present within a given range.

Code:

print("Enter a range:")
range1=int(input())
range2=int(input())
print("Evil numbers between ",range1," and ",range2," are: ")
for i in range(range1,range2+1):
one_c = 0
num=i
while num != 0:
if num % 2 == 1:
one_c += 1
num //= 2
if one_c % 2 == 0:
print(i,end=" ")

Input/Output:
Enter a range:
1000
1010
Evil numbers between  1000  and  1010  are:
1000 1003 1005 1006 1008

More:-