Solution Review : Check Parity of a Number
This lesson gives a detailed review of how to check the parity of a number.
We'll cover the following
Solution: Use Modulus(%)
Operator
The solution isn’t complicated. You simply check the remainders when even and odd numbers are divided by 2; all even numbers result in a remainder of 0 and all odd numbers produce a remainder of 1. This is convenient because the problem requires us to return a 0 if a number is even, and a 1 if it is odd.
Therefore, you will just use the %
operator to obtain the remainder of n when divided by 2 and return the answer.
Get hands-on with 1200+ tech skills courses.