# Simple but Nagging Question HELP!

• 11-22-2011
Dr.Tautology
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.
• 11-22-2011
rags_to_riches
((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.
• 11-22-2011
Dr.Tautology
So it is basing the evaluation on whatever the remainder of the division is? 2%2=1 anywhere on planet earth.
• 11-23-2011
rags_to_riches
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.
• 11-23-2011
Dr.Tautology
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?
• 11-23-2011
StainedBlue
% 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
• 11-23-2011
Dr.Tautology
Got it. That makes sense. Don't know why my book hasn't mentioned the difference between / and %.
• 11-23-2011
manasij7479
Quote:

Originally Posted by Dr.Tautology
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.
• 11-23-2011
Dr.Tautology
Problem solving with C++ 7th ed. Walter Savitch.