MeetCode - Programming Platform | MeetCode - Programming Solutions Platform

C++ Program to Check Even or Odd Number Using C++ with Explanation

C++ Medium Variables 22 views
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
Back to Questions

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; }

Output Example

Input:
8
Output:
Even Number

Time & Space Complexity

Time
O(1)
Space
O(1)

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

Solutions (0)

No solutions submitted yet. Be the first!

Prev Next