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. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    3

    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. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Think of the REMAINDER left over, not the answer:

    2/2 = someAnswer we don't care about. WITH REMAINDER OF ZERO


    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. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    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. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    3
    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. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Quote Originally Posted by salemseven View Post
    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. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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. #7
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    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.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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
    Last edited by CommonTater; 02-21-2011 at 03:23 PM.

  9. #9
    Registered User
    Join Date
    Feb 2011
    Posts
    3
    Thank you so much!
    My book "Learn C on the Mac" was just too brief ... I am finding....
    This forum is awesome!

  10. #10
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Quote Originally Posted by salemseven View Post
    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
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MFC newbie --- simple problem
    By gemini_shooter in forum Windows Programming
    Replies: 3
    Last Post: 04-03-2006, 04:17 PM
  2. Need some really simple help (complete Linux newbie)
    By Hannwaas in forum Linux Programming
    Replies: 11
    Last Post: 12-10-2001, 02:16 PM
  3. Im a Newbie with a graphics design problem for my simple game
    By Robert_Ingleby in forum C++ Programming
    Replies: 1
    Last Post: 11-23-2001, 05:41 PM
  4. Replies: 8
    Last Post: 11-20-2001, 11:13 PM
  5. Replies: 3
    Last Post: 11-04-2001, 02:53 PM

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