Cpp Examples Even Odd
# C++ Example - Determine if a Number is Odd or Even
[ C++ Examples](#)
Below we use % to determine if a number is odd or even. The principle is to divide the number by 2; if the remainder is 0, it is even, otherwise it is odd.
## Example - Using if...else
#includeusing namespace std; int main(){int n; cout<>n; if(n % 2 == 0)cout<<n<<" is even."; else cout<<n<<" is odd."; return 0; }
The output of the above program is:
input an integer.: 55 is odd.
## Example - Using the Ternary Operator
#includeusing namespace std; int main(){int n; cout<>n; (n % 2 == 0) ? cout<<n<<" is even." : cout<<n<<" is odd."; return 0; }
The output of the above program is:
input an integer.: 55 is odd.
[ C++ Examples](#)
YouTip