How do u use........

This is a discussion on How do u use........ within the C++ Programming forums, part of the General Programming Boards category; How do you use % to determine if a number is even. I don't understand....

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    6

    Question How do u use........

    How do you use % to determine if a number is even. I don't understand.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >How do you use % to determine if a number is even. I don't understand.
    Code:
    if ( val % 2 == 0 )
      // Even
    Divide the value by two, if the remainder is 0 then the value was evenly divided by two and is thus an even number. The % operator saves you from performing several operations in that it simply returns the remainder.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21