1. ## if statement

I came across this program in my book and read about it.

Code:
```#include <iostream>

using namespace std;

int main()
{
long number=0;
cout << endl
<< "Enter an integer number less than 2 billion: ";
cin >> number;

if(number % 2L)
cout << endl
<< "your number is odd." << endl;

else
cout << endl
<< "your number is even." << endl;
return 0;
}```
I don't quite understand the if(number % 2L) statement really means. A ran on google and went to a couple open chats about it and got nothing. All I can make of this is.

The if statment runs starts the if loop.

Number is the variable.

What does the % 2L mean?

2. % is the modulus operator.

What this does is it gives you the remainder of the number divided by whatever number is to the right.

The L tells the system that the number 2 should be represented as a long instead of an int, which is what it would treat it as if you didn't add the L.

Quzah.

3. % (Modulus) gives the remainder after a division operation. So if number isn't a multiple of 2, the if statement will be true. For example: 4 % 2 = 0, as 4 goes evenly into 2. 5 % 2 = 1, as 5 doesn't go evenly into 2. Basically, number % 2 tests whether a number is even or odd. The 'L' on the end of the 2 ensures the compiler treats it as a long.

edit: Beaten

4. Ok. So if(number % 2L) would mean divide my number by 2. and turn it into a long variable. So should my output be switched? So the correct code would have the else as odd and the even as if?

5. No, the modulus operator returns the remainder of a division. You're not actually changing the value or type of your variable. If (number%2L) resolves to 1, the first code will run. Otherwise, the second code will run.

6. Originally posted by ZakkWylde969
What's not to understand?

cout << "7 / 3 is " << 7 / 3 << endl << "7 % 3 is " << 7 % 3 << endl;

You do know basic division, right? Well when you aren't resolving to the decimal point, you use the remainder. The % is used to give you the remainder of a number divided by another.

Quzah.

7. Well they already explained it, but maybe more examples will help . Like they said above, % just gets the remainder of the devision operation. Therefore 10%2=0 and 10%3=1. 10/2 devides cleanly, no remainder, whereas 10/3 is 3 with a remainer of 1(hence 10%3=1). Whenever you do x%2, you are basically checking if the number is even or odd (even if the result is 0, odd if the result is 1). Well, hope that helped!

8. On a different note, I was thinking about even and odd numbers. In terms of binary, the difference between them is simply the first bit. So maybe a better way to check it is to check if (number&1) is true. Division is CPU intensive isn't it? A bitwise check should be quicker (technically, not practically).

9. I know division but I don't see how 10 / 2 equils 0. It equals five. I don't know what math calsses you guys took but sofar my teachers have tought me 10 split in 2 is five..

10. Originally posted by ZakkWylde969
I know division but I don't see how 10 / 2 equils 0. It equals five. I don't know what math calsses you guys took but sofar my teachers have tought me 10 split in 2 is five..
Re-read his post. He never said that 10/2 is zero. He said:
% just gets the remainder of the devision operation. Therefore 10%2=0
And then:
10/2 devides cleanly, no remainder,
Perhaps you need to pay more attention. Your problem would have been solved in the first reply.

Quzah.

11. I might just be retarded but I don't get a lick of this.

12. Originally posted by ZakkWylde969
I might just be retarded but I don't get a lick of this.
I'm inclined to agree. Did you even compile the simple cout line I gave you as an example? Perhaps programming is just not for you.

Quzah.

13. Originally posted by quzah
I'm inclined to agree. Did you even compile the simple cout line I gave you as an example? Perhaps programming is just not for you.

Quzah.
I'm glad I have your support. I'm just not understanding. You guys are explaining it over and over in the SAME words. MAybe a more detailed explanation would help other than it gives you the remainder divided by blah blah. I'm just not understanding.

14. Sorry Zakk, but there's just not many ways to explain it. Ok, you know division so you know that 10/5=2. You should also know that 10/5 doesn't have a remainder. If the remainder part is getting you, when dividing a remainder is what is left over. For example: 10/6=1 with a remainder of 4. 10/3=3 with a remainder of 1. 10/3 has a remainder of 1 because 3x3=9 and that leaves 1 left over(to make it = 10). After learning what a remainder is, please refer to my orignal post