# Program to Find nth Evil Number

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 nth Evil Number.

Data requirement:-

Input Data:- rangenumber

Output Data:- rangenumber, letest

Program in C

Here is the source code of the C Program to Find nth Evil Number.

Code:

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

}
if(one_c%2==0)
{
c++;
letest=num;
}
num = num + 1;
}
printf ("%dth Evil number is %d", rangenumber, letest);
return 0;
}

Input/Output:
Enter the Nth value:5
5th Evil number is 10

Program in C++

Here is the source code of the C++ Program to Find nth Evil Number.

Code:

#include <iostream>
using namespace std;
int
main ()
{
int i, rangenumber, num = 1, c = 0, letest = 0;
cout<<"Enter the Nth value:";
cin>>rangenumber;
while (c != rangenumber)
{
int num1=num;
int one_c=0;
while(num1!=0)
{
if(num1%2==1)
{
one_c++;
}
num1/=2;

}
if(one_c%2==0)
{
c++;
letest=num;
}
num = num + 1;
}
cout<<rangenumber<<"th Evil number is "<<letest;
return 0;
}

Input/Output:
Enter the Nth value:7
7th Evil number is 15

Program in Java

Here is the source code of the Java Program to Find nth Evil Number.

Code:

import java.util.Scanner;
public class NthEvilNumber {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int  rangenumber, num = 1, c = 0, letest = 0;
System.out.println("Enter Nth number:");
rangenumber=cs.nextInt();

while (c != rangenumber)
{
int num1=num;
int one_c=0;
while(num1!=0)
{
if(num1%2==1)
{
one_c++;
}
num1/=2;

}
if(one_c%2==0)
{
c++;
letest=num;
}
num = num + 1;
}
System.out.println(rangenumber+"th Evil number is "+letest);
cs.close();
}
}

Input/Output:
Enter Nth number:
10
10th Evil number is 20

Program in Python

Here is the source code of the Program to Find nth Evil Number.

Code:

rangenumber=int(input("Enter a Nth Number:"))
c = 0
letest = 0
num = 1
while c != rangenumber:
one_c = 0
num1 = num
while num1 != 0:
if num1 % 2 == 1:
one_c += 1
num1 //= 2
if one_c % 2 == 0:
c+=1
letest = num

num = num + 1
print(rangenumber,"th Evil number is ",latest)

Input/Output:
Enter a Nth Number:15
15 th Evil number is  30

More:-