Skip to content
C++ Better Explained
Go back
C++ Even or Odd Program: Check Any Number (3 Ways)
Edit page

C++ Even or Odd Program

Checking whether a number is even or odd is one of the first real programs every C++ beginner writes. It’s small, but it teaches the modulo operator, conditionals, and reading input — three skills you’ll use forever. Let’s build it three ways.


Method 1: The Modulo Check

An even number divides by 2 with nothing left over. The modulo operator % gives the remainder, so number % 2 == 0 means “even”:

#include <iostream>

int main() {
    int number = 7;
    if (number % 2 == 0)
        std::cout << number << " is even\n";
    else
        std::cout << number << " is odd\n";
    return 0;
}

7 % 2 is 1 (odd), so this prints “7 is odd.” Change number to 8 and the remainder becomes 0, so it prints “even.” That % 2 == 0 test is the heart of the whole program.


Method 2: Ask the User

A program is more useful when it works on any number the user types. Read it with std::cin:

#include <iostream>

int main() {
    int number;
    std::cout << "Enter a number: ";
    std::cin >> number;

    if (number % 2 == 0)
        std::cout << number << " is even\n";
    else
        std::cout << number << " is odd\n";
    return 0;
}

std::cin >> number waits for the user to type a value and stores it. Everything else is the same — the logic doesn’t care where the number came from.

If you're looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you're a complete beginner or looking to solidify your understanding. Just $19.

Method 3: The One-Line Ternary

Once the idea is clear, you can collapse the if/else into a single line with the ternary operator:

#include <iostream>
#include <string>

int main() {
    int number = 13;
    std::string result = (number % 2 == 0) ? "even" : "odd";
    std::cout << number << " is " << result << "\n";
    return 0;
}

The ternary picks "even" when the test is true and "odd" when it’s false. It’s the same logic, just more compact — handy once you’re comfortable with the pattern.


Reusing It with a Function

If you check even/odd in several places, wrap it in a function so you write the logic once:

#include <iostream>

bool isEven(int n) {
    return n % 2 == 0;
}

int main() {
    for (int i = 1; i <= 5; ++i)
        std::cout << i << ": " << (isEven(i) ? "even" : "odd") << "\n";
    return 0;
}

isEven returns a bool you can use anywhere a condition is expected. This is a great early example of why functions make code cleaner and easier to read.


Bonus: The Bitwise Shortcut

Experienced programmers sometimes use & 1 to test the last bit, which is 0 for even numbers:

#include <iostream>

int main() {
    int number = 42;
    // The last bit is 0 for even numbers, 1 for odd
    if ((number & 1) == 0)
        std::cout << "even\n";
    else
        std::cout << "odd\n";
    return 0;
}

This is slightly faster, but % 2 is just as efficient on modern compilers and far easier to read. Stick with modulo unless you have a specific reason not to.



Take Your C++ Further

If you’re looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you’re a complete beginner or looking to solidify your understanding. Just $19.

👉 Get the C++ Better Explained Ebook — $19

📋

Free Download: The 10 Mistakes Every C++ Beginner Makes

A free 1-page checklist that shows the exact traps that slow down every C++ beginner — so you can avoid them from day one.

🔒 No spam. Unsubscribe anytime.


Edit page
Share this post on:

Previous Post
C++ char to int Conversion: The Right Way (and the Common Trap)
Next Post
C++ Exponents: Why ^ Isn't Power and How to Use pow()