# Newbie: simple math using % - HELP

This is a discussion on Newbie: simple math using % - HELP within the C Programming forums, part of the General Programming Boards category; Hi - this may be a very simple answer but I just cannot see it. I am also just beginning ...

1. ## Newbie: simple math using % - HELP

Hi - this may be a very simple answer but I just cannot see it. I am also just beginning C programming by book and have no one to turn to but this very helpful forum. Thank You.
Here is source:

#include <stdio.h>

int main (int argc, const char * argv[]) {
int i;

for ( i = 1; i <= 20; i++ ) {
printf( "The number %d is ", i );

if ( (i % 2) == 0 )
printf( "even" );
else
printf( "odd" );

if ( (i % 3) == 0 )
printf( " and is a multiple of 3" );

printf( ".\n" );
}

return 0;
}

Here is my problem with the above code:

If the % operator is doing math - and if i is being divided by 2 "leaving a remainder?" and if that remainder is even than it will be 0 and if odd it will be 1

Should I look at it like this?

i-1 ---- so 1/2= .5 thus .5=odd ---remainder = odd
i=2 ---- so 2/2=1 thus 1=odd --- remainder = odd?

Iam confused

2. Think of the REMAINDER left over, not the answer:

When you divide 1/2, you don't get .5 - that's not an integer (whole number please). You get zero, with one remainder.

3. No. Remainder is more like

1/2 = 0, so what's left is 1
2/2 = 1, so what's left is 0

We're always dealing with integers. The divisor will go into the numerator wholly so many times...

4. Wow! Thanks for the quick reply!

So should I look at the % operand like this?
If there is no remainder = 0 and is even
If there is a remainder =1 and is odd

So if we continue:
3/2 = 1-1/2 =there is a remainder so i=odd
4/2 =2 =no remainder so i=even
and so on?

Here is what my book says:
This next chunk of code introduces a brand-new operator. % is a binary operator that returns the remainder when the left operand is divided by the right operand. For example, i % 2 divides 2 into i and returns the remainder. If i is even, this remainder will be 0. If i is odd, this remainder will be 1.
if ( (i % 2) == 0 ) printf( "even" );
else printf( "odd" );

5. Originally Posted by salemseven
So if we continue:
3/2 = 1-1/2 =there is a remainder so i=odd
4/2 =2 =no remainder so i=even
and so on?
That's pretty much it. A remainder in the case of dividing by two would indicate it's odd.

If you do a modulo 3 operation %3, the remainder can be 0, 1, or 2. 0 means it divided cleanly by 3.

6. Integer division doesn't make an answer of anything else except whole numbers. When you say 3/2 is one and a half -- NOPE!

<< THE ANSWER TO INTEGER DIVISION IS AN INTEGER. NEVER A FRACTION >>

if((i % 2) == 0) is a common C idiom to test if i is even, but don't connect that too strongly with the modulo operator %.

Modulo doesn't know or care anything about odd or even, or any of that. It's is simply

<< THE REMAINDER >>

of a division, and nothing else.

7. The percent sign is the modulo operator. If I assign x like this,

x = (16 % 3);

x will be assigned the value of 1, since 16 divided by 3 leaves a remainder of 1. So if any number divided by two has a remainder of zero, that number is even. If a number divided by three has a remainder of zero, that number is a multiple of three.

8. Think of it this way...

mod = x - ( ( x / y ) * y )

mod = 16 - (16/3) *3) = 16 - (5 *3) = 16 - 15 = 1

16 mod 3 = 1

9. Thank you so much!
My book "Learn C on the Mac" was just too brief ... I am finding....
This forum is awesome!

10. Originally Posted by salemseven
Thank you so much!
My book "Learn C on the Mac" was just too brief ... I am finding....
This forum is awesome!
You're welcome.

Lol, they have C programming books specifically for Mac :P