Check Odd Number in C#

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 2 Code Examples
Parity

What you’ll learn

  • What “odd” means for integers and how that maps to n % 2 != 0.
  • How to wrap the test in a reusable IsOdd helper that returns true or false.
  • Two programs: check one value (like 15) and list odds from 1 to 10, plus a live preview.
  • Why zero is not odd and how C# remainder works for negative odds.

Overview

Odd integers are those not divisible by 2. The classic C# interview check is number % 2 != 0: if the remainder after dividing by 2 is not zero, the number is odd.

Two small programs

One checks a single value (15), the other lists odds from 1 to 10.

Live preview

Try 15, 0, 10, or -3 and see the remainder plus verdict.

Pair with even

Odd and even split every integer — learn both checks for interview parity questions.

Prerequisites

Basic C#: Main, console output, if/else, and for loops.

  • The remainder operator % and comparison operators.
  • Methods that return bool (optional but helpful).

What is an odd number?

An integer n is odd when it is not divisible by 2. Dividing by 2 leaves a remainder of 1 (for positive odds) or -1 (for some negative odds in C#).

On the number line the odds hop by twos starting at 1: …, -5, -3, -1, 1, 3, 5, …. In code, n % 2 != 0 captures that rule for every int.

Odd n % 2 != 0
Even n % 2 == 0
Zero not odd

Remainder after dividing by 2

Odd integers leave a nonzero remainder when divided by 2. Even integers leave remainder 0. The two categories partition every integer — nothing is both, and every integer is one or the other.

Quick example: 15

15 ÷ 2 = 7 with remainder 1, so 15 is odd. In C#: 15 % 2 != 0 is true.

Intuition

15 Odd
Remainder
15 % 2 is 1
Code check
15 % 2 != 0 → true
10 Even
Remainder
10 % 2 is 0
Verdict
not odd

Takeaway: parity checks are among the first programs beginners write — master them before harder number puzzles.

Live preview

Runs in your browser with the same idea as IsOdd: remainder not equal to 0 means odd.

Try 0 (not odd) or -3 (odd).

Live result
Press “Check parity” to classify.

Algorithm

Goal: decide whether a given integer n is odd.

Compute n % 2

If the remainder is not 0, n is odd. In C#: return n % 2 != 0;.

Optional: scan a range

Loop i from start through end inclusive; print i when IsOdd(i) is true.

📜 Pseudocode

Pseudocode
function isOdd(n):
    return (n mod 2) != 0

function printOddsInRange(start, end):
    for i from start to end:
        if isOdd(i):
            output i
1

Check a single number

IsOdd returns true or false. Main tests 15 and prints one friendly line — matches the classic reference output.

c#
using System;

class Program
{
    static bool IsOdd(int number)
    {
        return number % 2 != 0;
    }

    static void Main()
    {
        int number = 15;

        if (IsOdd(number))
            Console.WriteLine($"{number} is an odd number.");
        else
            Console.WriteLine($"{number} is not an odd number.");
    }
}

Explanation

IsOdd centralizes the rule; Main only branches on the boolean result.

return number % 2 != 0;

The rule. Nonzero remainder after dividing by 2 means odd.

if (IsOdd(number))

Readable flow. Swap 15 for any literal or parsed input to test other values.

2

Odd numbers between 1 and 10

Loop across an inclusive range and print values where IsOdd(i) is true — the reference range walkthrough.

c#
using System;

class Program
{
    static void Main()
    {
        int start = 1;
        int end = 10;

        Console.WriteLine($"Odd Numbers in the range {start} to {end}:");

        for (int i = start; i <= end; i++)
        {
            if (IsOdd(i))
                Console.Write($"{i} ");
        }

        Console.WriteLine();
    }

    static bool IsOdd(int number)
    {
        return number % 2 != 0;
    }
}

Explanation

The outer for visits every integer in the closed interval; the inner if keeps only odds. Change start and end to scan a different range.

Optimization and variations

Step by two. After aligning start to the first odd value, increment i += 2 to visit only odds — fewer iterations than testing every integer.

Bitwise parity. (n & 1) == 1 often matches oddness on typical int values. Prefer % while learning; use bitwise only when you understand two’s complement.

Interview tip: state that zero is even (not odd) and prefer != 0 over == 1 for signed integers.

❓ FAQ

An integer is odd when it is not divisible by 2 — dividing by 2 leaves a leftover. Examples include ..., -5, -3, -1, 1, 3, 5, ...
No. Zero is even. In C#, 0 % 2 != 0 is false, so IsOdd(0) returns false.
The % operator gives the remainder after division. Odd integers leave a nonzero remainder when divided by 2.
For nonnegative integers, yes. For all ints in C#, != 0 is safer because odd negatives can have remainder -1, not +1.
They are still odd. For example, -3 % 2 is -1 in C#, and -1 != 0 is true, so IsOdd(-3) is true.
Testing one integer is O(1) time and O(1) extra space. Printing every odd value between start and end needs O(end - start + 1) steps in the simple loop.

🔄 Input / output examples

Value of nIsOdd(n) in C#
15true
10false
0false (zero is even)
-3true (remainder -1)

Edge cases and pitfalls

Zero

n = 0

Zero is even, not odd. 0 % 2 != 0 is false.

Negatives

Odd negatives

-3 % 2 is -1 in C#. Use != 0, not == 1, so negatives classify correctly.

Bounds

Inclusive ranges

Use i <= end when the problem includes both endpoints.

⏱️ Time and space complexity

OperationTimeExtra space
IsOdd(n)O(1)O(1)
Range [start, end] with per-value testO(end - start + 1)O(1)

Summary

  • Definition: odd integers are not divisible by 2; check with n % 2 != 0.
  • Structure: wrap the test in IsOdd, branch or loop from Main.
  • Remember: zero is not odd; use != 0 for all signed integers.
Did you know?

Zero is not odd. In C#, 0 % 2 != 0 is false because zero is even. Odd numbers leave a nonzero remainder when divided by 2.

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