Evil Number in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

/* C — Evil Number */
#include <stdio.h>
int main(){
    long long n; if(scanf("%lld",&n)!=1) return 0;
    if(n<0) n=-n;
    int cnt=0;
    while(n){ cnt += (n&1); n >>= 1; }
    printf((cnt%2==0) ? "Evil Number" : "Not Evil Number");
    return 0;
}

C Output

Input:
5

Output:
Evil Number


C++ Program

// C++ — Evil Number
#include <bits/stdc++.h>
using namespace std;
int main(){
    long long n; if(!(cin>>n)) return 0;
    n = llabs(n);
    int cnt=0;
    while(n){ cnt += (n&1); n >>= 1; }
    cout << (cnt%2==0 ? "Evil Number" : "Not Evil Number");
    return 0;
}

C++ Output

Input:
7

Output:
Not Evil Number


JAVA Program

// Java — Evil Number
import java.util.*;
class Main{
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    long n = sc.nextLong();
    if(n<0) n = -n;
    int ones = Long.bitCount(n);
    System.out.print((ones%2==0) ? "Evil Number" : "Not Evil Number");
  }
}

JAVA Output

Input:
0

Output:
Evil Number


Python Program

# Python — Evil Number
n = abs(int(input().strip()))
ones = n.bit_count()  # Python 3.8+: fast popcount
print("Evil Number" if ones % 2 == 0 else "Not Evil Number")

Python Output

Input:
10

Output:
Evil Number


In-Depth Learning – Entire Concept in Paragraphs
Example
Take 5. In binary, 5 = 101, which has two ones. Two is even, so 5 is an Evil Number. For 7, 7 = 111 has three ones; three is odd, so 7 is Not Evil (i.e., Odious).

Real-Life Analogy
Imagine a string of light switches (bits) where ON represents 1 and OFF represents 0. If the number of ON lights is even, an alarm stays calm (Evil). If the number is odd, the alarm buzzes (Odious). You’re not looking at where the switches are ON, just whether the total count is even or odd.

What We’re Really Checking (Logic)
Any integer can be represented in binary. An Evil Number is precisely one whose population count (also known as Hamming weight or set-bit count) has even parity. Therefore the problem boils down to:

Convert/examine the number's bits,

Count the 1s,

Check whether the count is even.
That is why the center of any solution is a loop (or built-in) for counting set bits, and then a parity test count % 2 == 0.

Learning Insights
This small exercise illustrates the bitwise mentality: how & 1 is used to access the least-significant bit, >> 1 is used to shift, and how many properties can be reduced to mere parity checks. It also illustrates the point that representation is important—we are not dealing with decimal digits here but with the machine's actual binary representation. In Python and Java, built-ins such as bit_count() and Long.bitCount() illustrate how languages reveal low-level activity with efficiency.

Why It Matters
Parity and popcount pop up all over: checksums, error-detecting codes (parity bits), cryptography, Bloom filters, bitmap indexes in databases, graphics masks, and CPU instructions such as POPCNT. Being able to count bits and check parity is a useful building block for performance-critical code.

Interview Relevance
Bit questions are interviewers' favorites because they expose proficiency with low-level operations. Potential follow-ups you could receive:

Count set bits without loops using tricks or built-ins.

Decide whether a number is odious (odd parity) or evil (even parity).

Calculate parity in O(1) through bit-folding methods.

Apply Kernighan's trick (n &= n-1) to find the number of bits set in O(k), where k is the number of bits set.

Edge Cases and Notes
Zero: 0 has binary 0 with zero 1s; zero is even → Evil.

Negative inputs: Code above handles n = abs(n) to not have sign issues; the "evil/odious" concept is conventionally used on non-negative integers.

Big values: All answers are O(number of bits). For 64-bit values that's constant-time in reality; built-ins are often optimized with CPU instructions.

Variations You Can Try
Sort a range [L, R] into Evil/Odious counts.

Print the binary representation alongside the label.

Use bitwise tricks to calculate parity quickly (e.g., XOR folding).

SEO-friendly wrap-up
This tutorial demonstrates how to test an Evil Number in C, C++, Java, and Python with rapid set-bit counting and straightforward parity testing of the binary form. You see how popcount is applied, why algorithms and systems need parity, and how to execute tidy, efficient bitwise logic in interviews and actual projects. If you are looking for "evil number program in C C++ Java Python," "check evil or odious number," or "count set bits and parity," the simple solutions and proper explanations below will assist you in becoming an expert with ease.