Check for Even Odd
Program to check if a number is even or odd
C++ Check for Even Odd Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#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;
}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:
Odd numbers:
Applications:
---
2. Header File: #include <iostream>
#include <iostream>
This header file provides:
cout → to print messages on the screencin → to take input from the userWithout this header, we cannot perform basic input and output in C++.
---
3. using namespace std;
For example:
std::cout, std::cincout, cinIt 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 variableThis variable will hold the number the user enters.
---
6. Taking Input From User
cin >> num;
numExample:
7
num = 7---
7. Understanding the Modulo Operator (%)
The expression:
num % 2
Key properties:
0
when divided by 2
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`):
If condition is false (`num % 2 != 0`):
Example with num = 7:
7 % 2 == 0 → 1 == 0 →false
else block"7 is odd"
Example with num = 10:
10 % 2 == 0 → 0 == 0 →true
if block"10 is even"
---
9. Complete Example Walkthrough
Example 1: Even number (10)
Step 1:
Input
10
num = 10Step 2:
Check condition
10 % 2 == 0 → 0 == 0 →true
Step 3:
Execute if block
"10 is even"
Example 2: Odd number (7)
Step 1:
Input
7
num = 7Step 2:
Check condition
7 % 2 == 0 → 1 == 0 →false
Step 3:
Execute else block
"7 is odd"
---
10. Why This Program is Important
Fundamental concept:
Real-world applications:
---
11. Alternative Approaches
Using bitwise AND:
if (num & 1 == 0) // Even
else // Odd
Our approach (modulo):
---
Summary
%) to check remaindernum % 2 == 0 means even, else oddThis program teaches:
Understanding even/odd checking is essential for:
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.
num = 8 → Output: "8 is even"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
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.