Thread: How to find a remainder of a division?

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    17

    How to find a remainder of a division?

    I want to find the remainder of the division between a and b, but without using the reminder operator a%b.
    I thought to subtract b from a as long as a>b, that will give the remainder, but I don't know how to write it in code. Please help!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suppose that you will be working with integers, in which case you could try and make use of integer division to compute the remainder, e.g., 7 / 3 = 2, 2 * 3 = 6, and 7 - 6 = 1, which is the remainder of the division.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    If a and b are both integral types (int, long, etc), use the expression a%b. The % operator is also known as modulo or remainder operator.

    If a or b are floating point, then look up the fmod() function in <math.h>
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Jun 2013
    Posts
    17
    thanks

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    without using modulo:

    Code:
    int a, b, quotient, remainder;
    
        quotient  = a/b;
        remainder = a - quotient*b;

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yeah, that was what I suggested in post #2, though I was hoping to leave the actual code as an exercise for the reader
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by laserlight View Post
    Yeah, that was what I suggested in post #2, though I was hoping to leave the actual code as an exercise for the reader.
    I thought the OP had given up and was just going to use % based on post #4, and this seemed more like an issue with the math itself than an issue with being able to implement the math as code.
    Last edited by rcgldr; 06-11-2013 at 03:29 PM.

  8. #8
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I thought the OP had given up and was just going to use % based on post #4, and this seemed more like an issue with the math itself than an issue with being able to implement the math as code.
    O_o

    Why then did you post code with no further explanation of the mathematics?

    Seriously, if you thought the example laserlight gave was insufficient why jump strait to posting code?

    *sigh*

    I think we have too many babysitters here these days.

    Soma

  9. #9
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by rcgldr View Post
    I thought the OP had given up and was just going to use % based on post #4, and this seemed more like an issue with the math itself than an issue with being able to implement the math as code.
    Quote Originally Posted by phantomotap View Post
    Why then did you post code with no further explanation of the mathematics?
    I thought that once the OP looked at the math (if the OP ever looks at this thread again), it would be obvious. The only further explanation would involve details such as if integer division rounds towards 0 (remainder has same sign as dividend), or round towards -∞ (remainder has same sign as divisor), which is probably beyond what is needed for this thread.

  10. #10
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    I thought that once the OP looked at the math (if the OP ever looks at this thread again), it would be obvious.
    O_o

    You think the mathematics was the issue? You didn't think this was a code issue?

    A descriptive example of the mathematics had already been posted; you only posted code for such mathematics; you didn't elaborate, refine, or otherwise add to the mathematics example without simply jumping strait to posting code.

    So, you simply posted code because you thought having the code would make the code obvious?

    Yes, I imagine that is true.

    Have you ever heard: "Give a man a fish, you feed him for a day; teach a man to fish, you feed him for a lifetime."?

    Or less "folksy": "It is more worthwhile to teach someone to do something, than to do something for them."?

    The purpose of the comments laserlight made was to nudge the original poster in the right direction. You have only banged the original poster over the head with an answer.

    If, by using the comments laserlight made, the original poster discovers the pattern of relationships between what is known and what is needed the individual will learn to guide themselves for such relationships as they find.

    You have instead taught that you are willing to throw code at people thus doing their work for them.

    Soma

  11. #11
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Again, I thought the OP had simply given up. I would have no problem if a moderator wants to delete posts # 5 through #11 (this is post #11), so the thread is back to where it was the last time the OP looked at this thread, or just edit post #5 and replace it with "example code removed".
    Last edited by rcgldr; 06-11-2013 at 05:20 PM.

  12. #12
    Registered User
    Join Date
    Oct 2011
    Location
    Las Vegas, NV
    Posts
    10
    The easy answer: use the ldiv function provided by the standard library.

    ldiv - Stdlib.h - C - C++ Computing Reference with Worked Examples

    Of course the point of this problem might be to learn how the modulo operator works, but if not....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. manipulating the remainder of a float
    By thestien in forum C++ Programming
    Replies: 5
    Last Post: 10-04-2006, 06:55 AM
  2. how to get remainder of a value
    By seal in forum C Programming
    Replies: 2
    Last Post: 09-22-2005, 07:03 AM
  3. C++ math operators (the remainder one)
    By Geo-Fry in forum C++ Programming
    Replies: 14
    Last Post: 03-27-2003, 10:41 PM
  4. Find integer 1- 1000 w/ most divisors w/o remainder
    By AlexDeToi in forum C++ Programming
    Replies: 3
    Last Post: 03-24-2002, 08:45 PM
  5. Determining if a number has a remainder?
    By Captain Penguin in forum C Programming
    Replies: 3
    Last Post: 09-01-2001, 07:07 AM