Telling the Difference between Even and Odd numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sonic
    New Member
    • Nov 2006
    • 40

    Telling the Difference between Even and Odd numbers

    During input validation, how would one decipher between even and odd numbers? I have to write a program that allows an individual to select numbers between 2-14, excluding odds. Here is a snippet from that code line:

    if (usersel < 2 || usersel >14)
    cout << "Invalid input. Please try again.";


    What would I place in the third or comparison to weed out the odd numbers?
    Thank you for taking the time to read this.
  • DavidJones
    New Member
    • Nov 2006
    • 6

    #2
    Hi,

    Use modulus operator, %. It computes the remainer that results from performing integer division.

    if(number % 2 == 0) => number is even
    else number is odd.

    ~David

    Comment

    Working...