In this C++ example, we’ll learn How to check whether number is even or odd.
if…else statement is used to check whether a number entered by the user is even or odd.
C++ Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> using namespace std; int main() { int n; cout << "Enter an integer: "; cin >> n; if ( n % 2 == 0) cout << n << " is even."; else cout << n << " is odd."; return 0; } |
Output: