In this tutorial, we will write a C++ program to check if a given integer is even or odd. The following topic is used in the program, so you may go through them.
Explanation: If the number is divisible by 2 and leaves the remainder 0 then the number is an even number else a number is an odd number.
The program takes a number from the user as input and checks that number for odd or even. If..else statement is used to compute the result. Lastly, the result is displayed.
C++ Program to Check Whether Number is Even or Odd usinf if else
Source code: Even and Odd Program in C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include <iostream> using namespace std; int main() { int num; cout << "Enter an integer: "; cin >> num; if ( num % 2 == 0) cout << num << " is an even number."; else cout << num << " is an odd number."; return 0; } |
Output:
//Run 1
Enter an integer: 20
20 is an even number.
//Run2
Enter an integer: 13
31 is an odd number.