modulus operator / '%' question

This is a discussion on modulus operator / '%' question within the C Programming forums, part of the General Programming Boards category; Gosh this is such a simple question but dont quite understand the logic. I understand that the modulus operator is ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    16

    modulus operator / '%' question

    Gosh this is such a simple question but dont quite understand the logic. I understand that the modulus operator is represented by '%'. And I understand that the expression x % y produces the remainder when x is divided by y.

    So when I divide 74 by 5 the answer is 14.8 . So I thought I should see the answer 8 but instead I see 4. Somebody please explain to me why there is a 4 instead of 8?

    [output]

    4

    [/output]


    Code:
    #include <stdio.h>
    
    
    main()
    {
      int it;
      it = 74 % 5;
      
      printf("%d\n", it);
      
    }

  2. #2
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    The % operator is defined as the integer remainder (modulus) of integer division.

    If you have integers A and B, where C = int(A / B), you can see the remainder, R, as being the result of the expression R = A - (B * C).

    What you were expecting was not the remainder, but the fractional part of a floating point result of a division between two whole numbers. That's not what % gives you.
    Last edited by filker0; 01-17-2006 at 10:12 AM. Reason: Add omitted words
    Insert obnoxious but pithy remark here

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,589
    Because modulus is the remainder of division, not a fraction.
    But 0.8 is also 4 / 5

    modulus = x - ( (x / y) * y ), when performed in integer arithmetic.

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    (a/b)*b + a % b = a
    Or with decimals
    14.8 is equal to 14 and eight tenths, eight is not the remainder of anything.
    Eight tenths is equal to 4/5, that is the remander(4 or 74 % 5) divided by 5.
    For integers divided by a power of 10 the numbers after the decimal point are the remainder
    74 / 10 = 7.4
    74 % 10 = 4

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    16
    got it - thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. using modulus & weighting factors
    By task in forum C Programming
    Replies: 4
    Last Post: 09-11-2002, 05:52 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

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