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
Show 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.