Table of contents

C++ Program to Check Whether Number is Even or Odd

Write a C++ program that checks if a given integer is even or odd.

Input and Output Examples

  • Input: 4
    Output: 4 is even.
  • Input: 7
    Output: 7 is odd.

Algorithm:

  1. Start the Program: Include necessary headers and declare the use of the standard namespace.
  2. Prompt for Input: Request the user to enter an integer.
  3. Read the Input: Store the integer in a variable.
  4. Check Even/Odd: Determine if the number is even or odd using the modulus operator.
  5. Display the Output: Print whether the number is even or odd.
  6. End the Program: Finish execution cleanly.

Below is a C++ program that uses the modulus operator to check if a number is even or odd:

cpp
#include <iostream> 
using namespace std; 

int main() {
    int num; // Declare an integer to store the user input.

    // Take input values
    cout << "Enter an integer: "; 
    cin >> num; 

    // Check if the number is even or odd using the modulus operator
    if (num % 2 == 0) {
        cout << num << " is even." << endl; // Output if the number is even.
    } else {
        cout << num << " is odd." << endl; // Output if the number is odd.
    }

    return 0; // End the program successfully.
}

Programming

Related Articles