C++ Program to Check Even or Odd Number Using C++ with Explanation
C++
Medium
Variables
22 views
2 min read
281 words
This problem helps you practice core C++ fundamentals in a practical way. It builds intuition around even, odd, check. Let’s break it down step by step so you can implement it confidently.
c++
variables
if-else
condition
medium
Problem Statement
In this problem, you will write a C++ program to check whether a given number is even or odd.
You will take an integer input from the user and use variables with conditional logic to decide the result.
This question helps you understand how variables work with if-else conditions in C++.
Input Format
One integer number.
Output Format
Print whether the number is even or odd.
Constraints
-10^6 ≤ number ≤ 10^6
Concept Explanation
In C++, variables are used to store input values.
To make decisions in a program, we use conditional statements like if and else.
An even number is a number that is completely divisible by 2.
An odd number is a number that is not completely divisible by 2.
To check this, we use the modulus operator (%).
The modulus operator gives the remainder after division.
• If number % 2 == 0, the number is even
• Otherwise, the number is odd
This type of logic is very common in programming.
It is used in validations, games, and real-world applications.
Step-by-Step Logic
Step 1: Understand the goal: check even or odd.
Step 2: Choose the main idea: if-else condition.
Step 3: Declare an integer variable.
Step 4: Take input using cin.
Step 5: Use modulus operator %.
Step 6: Apply if-else logic.
Step 7: Print the result.
Code Solution
This explanation is written for learning purposes and to help beginners understand the concept clearly.
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter a number: ";
cin >> number;
if (number % 2 == 0) {
cout << "Even Number";
} else {
cout << "Odd Number";
}
return 0;
}
Common Mistakes
- Misreading input/output format.
- Not handling constraints and edge cases.
- Off-by-one errors in loops.
- Forgetting to reset variables between test cases (if any).
Notes & Extra Practice
Examples
Input: 10
Output: Even Number
Input: 7
Output: Odd Number
Solution Guide
Tags
c++
variables
if-else
condition
medium
Problem
In this problem, you will write a C++ program to check whether a given number is even or odd.
You will take an integer input from the user and use variables with conditional logic to decide the result.
This question helps you understand how variables work with if-else conditions in C++.
Input / Output
Input
One integer number.
Output
Print whether the number is even or odd.
Constraints
-10^6 ≤ number ≤ 10^6
Examples
Input: 10
Output: Even Number
Input: 7
Output: Odd Number
Explanation
Concept Explanation
In C++, variables are used to store input values.
To make decisions in a program, we use conditional statements like if and else.
An even number is a number that is completely divisible by 2.
An odd number is a number that is not completely divisible by 2.
To check this, we use the modulus operator (%).
The modulus operator gives the remainder after division.
• If number % 2 == 0, the number is even
• Otherwise, the number is odd
This type of logic is very common in programming.
It is used in validations, games, and real-world applications.
Step-by-Step Explanation
Step 1: Understand the goal: check even or odd.
Step 2: Choose the main idea: if-else condition.
Step 3: Declare an integer variable.
Step 4: Take input using cin.
Step 5: Use modulus operator %.
Step 6: Apply if-else logic.
Step 7: Print the result.
Details
Common Mistakes
- Misreading input/output format.
- Not handling constraints and edge cases.
- Off-by-one errors in loops.
- Forgetting to reset variables between test cases (if any).
Official Solution
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter a number: ";
cin >> number;
if (number % 2 == 0) {
cout << "Even Number";
} else {
cout << "Odd Number";
}
return 0;
}
Solutions (0)
No solutions submitted yet. Be the first!