Write a C++ program to check if a number is even or odd.
Algorithm of program to check if a number is even or odd. Start Initialize: number (integer) Input: User inputs 42 number = 42 Processing: Calculate 42 % 2 which equals 0 Since 0 == 0, print “The number 42 is even.” End
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> using namespace std; int main() { // Declare a variable to store the number int number; // Get user input cout << "Enter a number: "; cin >> number; // Check if the number is even or odd if (number % 2 == 0) { // If the remainder when dividing by 2 is 0, the number is even cout << "The number " << number << " is even." << endl; } else { // If the remainder when dividing by 2 is not 0, the number is odd cout << "The number " << number << " is odd." << endl; } return 0; } |
Explanation of the Code Include Header Files: #include <iostream>: This header file …
Write a C++ program to check if a number is even or odd. Read More »