C# Odious Number Program

Beginner
6 min read
Updated: Nov 2026
2 programs
Live preview

What Is an Odious Number?

An odious number is a nonnegative integer whose binary form has an odd number of 1 bits. That count is called the popcount (Hamming weight). An even popcount means the number is evil.

Remember
n is odious  ⇔  (count of 1-bits in binary n) is odd

Examples:
   7 → 111  → 3 ones → odious
   8 → 1000 → 1 one  → odious
  15 → 1111 → 4 ones → evil
   0 → 0    → 0 ones → evil
nBinaryOnesResult
000Evil
111Odious
71113Odious
1511114Evil

How to Check an Odious Number in C#

Four clear steps — then pick a teaching loop or the built-in popcount.

  1. Require n >= 0 (this page rejects negatives).
  2. Count how many 1 bits appear in binary n.
  3. If that count is odd, n is odious; otherwise it is evil.
  4. Use a divide-by-2 loop to learn the idea, or BitOperations.PopCount for production code.
MethodIdeaTimeBest for
Divide by 2Read bits with % 2 / / 2O(bits)Interviews, teaching binary
BitOperations.PopCountBuilt-in popcount, then % 2O(1)Clear production C#

Pseudocode

Pseudocode
function IsOdious(n):
    if n < 0:
        return false

    ones = 0
    while n > 0:
        if (n mod 2) == 1:
            ones = ones + 1
        n = n / 2

    return (ones mod 2) == 1

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
GoalOdd count of binary 1 bits
OppositeEven popcount → evil
ZeroNot odious (0 ones is even)
Fast checkBitOperations.PopCount((uint)n) % 2 == 1
Binary stringConvert.ToString(n, 2)
Classic odious1, 2, 4, 7, 8, 11, 13
Classic evil0, 3, 5, 6, 9, 10, 15

Live Preview

Enter a nonnegative integer to see its binary form, popcount, and verdict.

Nonnegative whole numbers only. Tap a chip or type a value.

Result
n = 7
Binary = 111
Ones = 3 (odd)
Verdict: ODIOUS NUMBER

Worked Walkthrough — n = 7

Peel bits with % 2, count the ones, then check parity.

Calculation
7 in binary = 111

Step 1: 7 % 2 = 1 → ones = 1; n = 3
Step 2: 3 % 2 = 1 → ones = 2; n = 1
Step 3: 1 % 2 = 1 → ones = 3; n = 0

ones = 3 → odd → 7 is odious

C# Programs

Start with the divide-by-2 loop so you can show each bit. Then use BitOperations.PopCount when you want a short production check.

Example 1 — Divide-by-2 Loop

Read each bit with % 2, count the ones, then test whether that count is odd.

C#
using System;

class Program
{
    static bool IsOdious(int n)
    {
        if (n < 0)
        {
            return false;
        }

        int ones = 0;
        while (n > 0)
        {
            if (n % 2 == 1)
            {
                ones++;
            }
            n /= 2;
        }

        return ones % 2 == 1;
    }

    static void Main()
    {
        int number = 7;
        if (IsOdious(number))
        {
            Console.WriteLine(number + " is an Odious Number.");
        }
        else
        {
            Console.WriteLine(number + " is not an Odious Number.");
        }
    }
}

How It Works

  1. Reject negatives so the definition stays on nonnegative integers.
  2. n % 2 is the least significant bit; if it is 1, increment ones.
  3. n /= 2 shifts the remaining bits right (integer division).
  4. Repeat until n becomes 0.
  5. Return whether ones % 2 == 1. For 7, ones = 3 → odious.

Example 2 — BitOperations.PopCount

Let the runtime count the 1-bits, then check parity in one expression.

C#
using System;
using System.Numerics;

class Program
{
    static bool IsOdious(int n)
    {
        if (n < 0)
        {
            return false;
        }
        return BitOperations.PopCount((uint)n) % 2 == 1;
    }

    static void Main()
    {
        Console.WriteLine(IsOdious(7) ? "7 is odious" : "7 is evil");
        Console.WriteLine(IsOdious(15) ? "15 is odious" : "15 is evil");
        Console.WriteLine(IsOdious(0) ? "0 is odious" : "0 is evil");
    }
}

How It Works

  1. Cast to uint so popcount matches the nonnegative definition.
  2. BitOperations.PopCount returns the number of set bits.
  3. % 2 == 1 means that count is odd → odious.
  4. Same answers as the loop, with less code and no manual bit peeling.
  5. Use the loop in interviews when you must show the process; use this in real C#.

Edge Cases & Pitfalls

Check these before calling the solution done.

Zero

0 is not odious

Zero ones is an even count, so 0 is evil. It is not odious.

Odd vs odious

Different parity

n % 2 checks the value. Odious checks the popcount. Example: 2 is even but odious; 3 is odd but evil.

Negatives

Out of scope

Most definitions use nonnegative integers. Return false or reject the input.

Binary string

Count only ones

If you convert to a string, count '1' characters — not the string length.

Time and Space Complexity

ProgramTimeExtra space
Divide-by-2 loopO(bits)O(1)
BitOperations.PopCountO(1)O(1)

For a 32-bit int, the loop runs at most 32 steps. Popcount is a fixed-width hardware-friendly operation.

Key Takeaways

  • Definition: odd popcount → odious; even popcount → evil.
  • Loop: peel bits with % 2 and / 2, then test ones % 2 == 1.
  • Built-in: BitOperations.PopCount((uint)n) % 2 == 1.
  • Test set: 7 and 8 odious; 15 and 0 evil; 2 is even but still odious.

One line: count the 1-bits; if that count is odd, the number is odious.

Frequently Asked Questions

A nonnegative integer is odious if its binary form has an odd number of 1 bits (odd popcount).
If the binary 1-bit count is even, the number is evil — the opposite of odious.
No. Zero has zero 1-bits, and 0 is even, so 0 is evil.
7 in binary is 111, which has three 1-bits. Three is odd, so 7 is odious.
Odd/even looks at the value (n % 2). Odious/evil looks at the parity of the count of 1-bits. For example, 2 is even but odious (binary 10 has one 1-bit).
Counting bits by dividing by 2 is O(number of bits). BitOperations.PopCount is effectively O(1) for fixed-size integers.

Next: Factorial of a Number

Compute n! with an iterative loop (and watch overflow).

Factorial tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful