Like Tree1Likes
  • 1 Post By rags_to_riches

Simple but Nagging Question HELP!

This is a discussion on Simple but Nagging Question HELP! within the C++ Programming forums, part of the General Programming Boards category; I am completely new to programming and I have a elementary question about how to test for an even number. ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    6

    Angry Simple but Nagging Question HELP!

    I am completely new to programming and I have a elementary question about how to test for an even number. I understand that ((n%2)==0) tests this--because the number needs to be divisible by 2--but I don't understand why the boolean expression ==0 works, e.g 2%2 is not = to 0. I understand that == is a boolean expression, but the book I'm learning from explains it as still evaluating for equality, but provides either true or false as a value. What am I missing? I'm guessing that it has something to do with the remainder from the division being equal to 0, but I don't really understand why that would be? I've tried it using both int vars and double vars. Could someone please explain this to me? I've asked all of my programmer friends at work, searched the internet, and no one has given me a reasonable answer.

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652
    ((2%2) == 0)
    What is the result of 2%2? 0. (The remainder of 2 divided by 2 is 0)
    ((0) == 0)
    Does 0 == 0? Of course, so it evaluates as a boolean true.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    6
    So it is basing the evaluation on whatever the remainder of the division is? 2%2=1 anywhere on planet earth.

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652
    The remainder of 2 divided by 2 is 1? Not on the my planet Earth. 2%2 returns the remainder of the division, not the result of the division.
    Elysia likes this.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    6
    I don't think we are understanding each other. The resut of 2%2, that is 2 divided by 2, is 1. This is obvious. What is not obvious is that it returns the remainder not the result. Maybe I am incorrect, but wouldn't the non boolean 2%2=1 be correct. That is, = is determined by the result not the remainder of the division, where as == is determined by the remainder, not the result of the division?

  6. #6
    ...and never returned. StainedBlue's Avatar
    Join Date
    Aug 2009
    Posts
    168
    % means modular division i.e. returns the remainder

    / means divide as in regular division

    == tests equality

    = assigns a value

    2%2 == 0

    2/2 == 1

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    6
    Got it. That makes sense. Don't know why my book hasn't mentioned the difference between / and %.

  8. #8
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by Dr.Tautology View Post
    Got it. That makes sense. Don't know why my book hasn't mentioned the difference between / and %.
    Which book..?
    All beginners' books I've seen; has a huge table clarifying those.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    6
    Problem solving with C++ 7th ed. Walter Savitch.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a simple question. plz help
    By monashhros3 in forum C Programming
    Replies: 1
    Last Post: 03-28-2008, 01:40 PM
  2. simple simple design question
    By Chaplin27 in forum C++ Programming
    Replies: 6
    Last Post: 05-31-2005, 11:33 PM
  3. Simple question
    By pianorain in forum C++ Programming
    Replies: 5
    Last Post: 01-14-2003, 04:54 PM
  4. Simple Question
    By John22 in forum C Programming
    Replies: 3
    Last Post: 12-14-2002, 09:41 AM
  5. My Mnemonic nagging.
    By sean in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 12-21-2001, 11:42 AM

Tags for this Thread


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