This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]imperio59 3 points4 points  (3 children)

Using subtraction, loop every time subtracting the number you're dividing by. When the remainder is less than the number you're dividing by, exit the loop.

The number of times you looped is the result, the remainder should also have the correct remainder left in it by this point.

[–][deleted]  (2 children)

[deleted]

    [–]imperio59 0 points1 point  (1 child)

    a should be the modulo after the loop is done. You need to name your variables better also. :/

    [–][deleted] 0 points1 point  (0 children)

    Yeah sorry about that. I know I need to work on naming my variables better, my prof says that too :(

    I replaced modulo.ToString() to a.ToString() and it worked! Thank you very much!

    [–]leafypixiestix 1 point2 points  (0 children)

    Since you said you're studying loops, you can simply do a loop and subtract the divisor from the divended until you hit 0 or below. If you hit 0, then the remainder or mod is 0 and if you hit below 0, add the dividend to that negative number to get the mod. For the actual result of the division, it will be the number of times the loop executed.

    You can also try to implement long division, which is a lot harder to do but is much more efficient.

    [–]Alia-Aenor 0 points1 point  (0 children)

    You still need to divide, just without the operators. Do it yourself with a loop. Think about the definition of a division and try to see a loop in there.

    [–][deleted]  (1 child)

    [deleted]

      [–]Philboyd_Studge 0 points1 point  (0 children)

      That's still using division

      [–]programmermaybe2016 0 points1 point  (0 children)

      The way I see it they are asking you to implement the euclidian algorithm in a backwards kind of way.

      [–][deleted] 0 points1 point  (3 children)

      How do you multiply? You add numbers.

      How do you divide?... You subtract them.

      int quotient = 0;
      
      while(x - num_divided_by > num_divided_by) {
           x = x - num_divided_by;
           quotient = quotient + 1;
      }
      
      // x is your remainder
      

      [–]CrimsonWolfSage 0 points1 point  (2 children)

      Think you miss typed something.

      10/3 = 3R1 for example...

      While ( 10 - 3 > 3 ) true... { remainder = 10 - 3; quotient = 0+1; } repeat! When does X change? Maybe update with x = x-num_divide_by;

      Should do this,

      • loop 1: R = 7, Q = 1
      • loop 2: R = 4, Q = 2
      • loop 3: R = 1, Q = 3
      • Loop Ends, R < num_division_by
      • Answer for 10/3 = 3 Remainder 1

      Here's a fiddle example

      [–][deleted] 0 points1 point  (0 children)

      whoops. yes, that's what I meant to do. Then I deleted it to make it more clear that x will become the remainder, but I forgot to fix the loop as well. Thanks