Check for Even Odd

Program to check if a number is even or odd

BeginnerTopic: Conditional Programs
Back

C++ Check for Even Odd Program

This program helps you to learn the fundamental structure and syntax of C++ programming.

Try This Code
#include <iostream>
using namespace std;

int main() {
    int num;
    
    cout << "Enter a number: ";
    cin >> num;
    
    if (num % 2 == 0) {
        cout << num << " is even" << endl;
    } else {
        cout << num << " is odd" << endl;
    }
    
    return 0;
}
Output
Enter a number: 7
7 is odd

Understanding Check for Even Odd

This program determines whether a number is even or odd. This is one of the most fundamental programs in programming, teaching basic conditional logic, the modulo operator, and decision-making. Understanding even/odd is essential for many algorithms and problem-solving scenarios.

---

1. What are Even and Odd Numbers?

Even numbers:

Divisible by 2 without remainder
Examples: 0, 2, 4, 6, 8, 10, 12, ...
When divided by 2, remainder is 0

Odd numbers:

Not divisible by 2 (leave remainder 1)
Examples: 1, 3, 5, 7, 9, 11, 13, ...
When divided by 2, remainder is 1

Applications:

Alternating patterns in programming
Array indexing (even/odd positions)
Mathematical problems
Many algorithmic challenges

---

2. Header File: #include <iostream>

#include <iostream>

This header file provides:

cout → to print messages on the screen
cin → to take input from the user

Without this header, we cannot perform basic input and output in C++.

---

3. using namespace std;

This line saves us from writing `std: :` before every C++ object.

For example:

Without it → std::cout, std::cin
With it → cout, cin

It makes the code easier for beginners.

---

4. Starting the Program: int main()

Every C++ program begins from the main() function.

Everything inside the curly braces { } is what the computer will run.

---

5. Declaring a Variable

int num;

Here:

int means the variable will store a whole number (integer)
num is the name of the variable

This variable will hold the number the user enters.

---

6. Taking Input From User

`cout << "Enter a number: ";`

cin >> num;

First line prints a message asking the user to input a number
Second line takes the user's input and stores it inside num

Example:

User types

7

num = 7

---

7. Understanding the Modulo Operator (%)

The expression:

num % 2

means: "Divide num by 2 and give me the remainder."

Key properties:

Even numbers always leave remainder

0

when divided by 2

Odd numbers always leave remainder

1

when divided by 2

Examples:

10 % 2 → 0 (10 is even)
13 % 2 → 1 (13 is odd)
0 % 2 → 0 (zero is even)
7 % 2 → 1 (7 is odd)

This is the key logic behind the program.

---

8. The if-else Condition

if (num % 2 == 0)

cout << num << " is even" << endl;

else

cout << num << " is odd" << endl;

How it works:

If condition is true (`num % 2 == 0`):

Number is even
Print: "num is even"

If condition is false (`num % 2 != 0`):

Number is odd
Print: "num is odd"

Example with num = 7:

Check: 7 % 2 == 01 == 0

false

Execute else block
Output:

"7 is odd"

Example with num = 10:

Check: 10 % 2 == 00 == 0

true

Execute if block
Output:

"10 is even"

---

9. Complete Example Walkthrough

Example 1: Even number (10)

Step 1:

Input

User enters:

10

num = 10

Step 2:

Check condition

10 % 2 == 00 == 0

true

Step 3:

Execute if block

Print:

"10 is even"

Example 2: Odd number (7)

Step 1:

Input

User enters:

7

num = 7

Step 2:

Check condition

7 % 2 == 01 == 0

false

Step 3:

Execute else block

Print:

"7 is odd"

---

10. Why This Program is Important

Fundamental concept:

Modulo operator is used in many programs
Even/odd checking appears in many algorithms
Conditional logic is essential in programming

Real-world applications:

Alternating row colors in tables
Checking array indices (even/odd positions)
Mathematical problems
Game logic (turn-based games)
Pattern generation

---

11. Alternative Approaches

Using bitwise AND:

if (num & 1 == 0) // Even

else // Odd

Checks last bit (even = 0, odd = 1)
Slightly faster, but less readable

Our approach (modulo):

More intuitive and readable
Easier for beginners to understand
Standard approach in most programs

---

Summary

Even numbers: divisible by 2 (remainder 0)
Odd numbers: not divisible by 2 (remainder 1)
Use modulo operator (%) to check remainder
num % 2 == 0 means even, else odd
This is a fundamental pattern used in many programs

This program teaches:

Modulo operator for remainder calculation
Conditional statements (if-else)
Decision-making in programming
Basic input/output operations

Understanding even/odd checking is essential for:

Many algorithmic problems
Pattern recognition
Array manipulation
Mathematical programming

This is one of the first programs every programmer learns, and the concepts (modulo, conditionals) are used throughout your programming journey.

---

8. Displaying the Result

cout prints the result to the screen.

If num = 8 → Output: "8 is even"
If num = 5 → Output: "5 is odd"

---

9. Ending the Program: return 0;

This tells the computer:

"The program ran successfully."

It is a standard way to end a C++ program.

---

Summary

You learned how to take input using cin.
You learned how to check even/odd using the modulo operator.
You learned how if–else conditions work.
You saw how cout prints messages to the screen.

This program builds the foundation for all future conditional logic programs in C++.

Let us now understand every line and the components of the above program.

Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.

Practical Learning Notes for Check for Even Odd

This C++ program is part of the "Conditional Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.

A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.

For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.

Table of Contents