Check if a Number is Even or Odd in C++

This exercise teaches you how to determine whether an integer number is even or odd using conditional statements in C++. It’s a fundamental concept that helps beginners understand how the modulus operator works and how to use `if` and `else` for decision making.

In this program, the user is asked to enter an integer. The number is then checked using the modulus operator (%) to see if it leaves a remainder when divided by 2. If the remainder is 0, it is an even number; otherwise, it is odd.

Key Concepts:
- Reading integer input from the user
- Using the modulus operator to check divisibility
- Applying conditional logic with `if-else` statements
- Displaying messages based on the condition outcome

Group

Introduction to C++

Objective

1. Declare an integer variable to store user input.
2. Prompt the user to enter an integer number.
3. Use the modulus operator (%) to check if the number is divisible by 2.
4. If the number is divisible by 2, print that it is even.
5. Otherwise, print that it is odd.

Read an integer and determine if it is even or odd.

Example C++ Exercise

 Copy C++ Code
#include <iostream> // Include the iostream library to enable input and output

using namespace std; // Use the standard namespace

// Main function - entry point of the program
int main() {
    int number; // Declare an integer variable to store the user input

    // Prompt the user to enter an integer number
    cout << "Enter an integer: ";
    cin >> number; // Read the number from user input

    // Use the modulus operator to check if the number is even or odd
    if (number % 2 == 0) {
        // If the remainder is 0, the number is even
        cout << "The number is even." << endl;
    } else {
        // Otherwise, the number is odd
        cout << "The number is odd." << endl;
    }

    return 0; // Return 0 to indicate successful execution
}

 Output

Enter an integer: 7
The number is odd.

Share this C++ Exercise


More C++ Programming Exercises of Introduction to C++

Explore our set of C++ Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C++. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C++.

  • Currency Converter: Euros to US Dollars in C++

    This exercise is designed to help you understand how to work with floating-point numbers and user input/output in C++. You will build a basic currency converter that takes an amoun...

  • Calculate the Perimeter of a Rectangle in C++

    This exercise focuses on computing the perimeter of a rectangle using basic arithmetic operations in C++. It’s a great way for beginners to get hands-on practice with input/output,...

  • Sum of the First N Natural Numbers in C++

    This exercise introduces the concept of summation using a loop in C++. You will create a program that calculates the sum of the first N natural numbers, where N is provided by the ...

  • Print the First 10 Fibonacci Numbers in C++

    This exercise helps you understand how to generate a sequence of numbers based on mathematical logic using iterative programming. Specifically, you'll write a program in C++ that p...

  • Hello World Program in C++

    The "Hello, World!" program is one of the simplest programs to write in any programming language. It serves as an introduction to the syntax and basic functionality of a programmin...

  • Sum of Two Numbers Entered by the User in C++

    This exercise introduces user input handling in C++ through a simple program that adds two numbers provided by the user. It's an ideal starting point for understanding how to work ...