2
package sola5;

import java.util.Scanner;

public class Assign1 {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.print("Enter a three digit integer: ");
        int a = input.nextInt();
        System.out.print("Now, enter a 2 digit integer: ");
        int b = input.nextInt();

        while (a > b) {
            a = a-b;        
            System.out.println();
        }
    }
}

This is as far as I have gotten. My next step is to make it display the answer as well as the remainder and I just cannot figure out how to do it.

PeteSola
  • 61
  • 1
  • 5
  • Print out `a`... Like: `System.out.println(a)` outside the loop – Andrew Li Sep 06 '16 at 02:36
  • First of all, please fix the title. "Divide by repeated division" sounds like a formula for infinite recursion... :) – ajb Sep 06 '16 at 02:48
  • But the real question is: Say a=50 and b=6. You've repeatedly subtracted b until a is 2, which is the remainder. But how do you figure out what the quotient is? (In English.) Once you're clear on how you compute that, it will be a lot easier for us to show you how to do it in code. – ajb Sep 06 '16 at 02:50
  • @ajb Thanks for pointing that out. Meant to put repeated subtraction. Also, I'm not entirely sure what you mean. I'm totally new to this whole coding thing and class was cancelled last week so I am beyond lost. After entering a, and then b, I would need the code to print out what the quotient as well as the remainder would be. – PeteSola Sep 06 '16 at 02:50
  • 4
    My question isn't really about coding at all. Say you did this on pencil and paper. You start with 50 and subtract 6 repeatedly. Somehow you need to figure out from this process, that the quotient is 8. How? Don't think about code. – ajb Sep 06 '16 at 02:57
  • @ajb Well because 6 x 8 equals 48, leaving a remainder of 2. – PeteSola Sep 06 '16 at 03:24
  • But if you use repeated subtraction, how would you get 8? Where would that come from? – ajb Sep 06 '16 at 03:25
  • @ajb I would have to subtract 6, 8 times. 9 times would put me at -4. – PeteSola Sep 06 '16 at 03:41
  • 1
    _Right_, it depends on how many times you subtract. That's what I was trying to get you to realize. So if the answer is how many times you subtract, then you just need to count how many times you subtract, in your code. That means that you declare a variable to use as a counter. Then, in the loop, whenever you do the subtraction, you add 1 to the counter. Then after the loop, the counter will be the quotient. – ajb Sep 06 '16 at 03:43
  • Great job of leading the horse to water, @ajb. This is what I'd call gentle, effective instruction. – duffymo Sep 06 '16 at 12:07
  • I appreciate you running me through the logic behind it, @ajb. Instead of just straight up doing the code for me. – PeteSola Sep 06 '16 at 14:11

2 Answers2

3

~~>Way 1

 int count=0;
  while(a>b){  
     a-=b;//similar to a = a - b
     ++count;//similar to count = count + 1;
    } 

System.out.println(a); // remainder
System.out.println(count);//quotient

~~> Way 2(Preffered)

(assume a=30 and b=4):

! Using % to get remainder so (a % b) = 30 % 4 = 2;

!Using ÷ to get the quotient you can do (int) a ÷ b = 30 ÷ 4 = 7;

Edit:

Read this nice link offered by @Spektre in Comments

Community
  • 1
  • 1
GOXR3PLUS
  • 6,877
  • 9
  • 44
  • 93
  • @Spektre now i have lucky strike 777 :) – GOXR3PLUS Sep 06 '16 at 05:53
  • 1
    I clear out the unnecessary comments (other readers does not need them anymore) ... btw there are much more options then just two if you're interested I recommend to read this related QA: [What is time complexity of divison?](http://stackoverflow.com/a/34352110/2521214) – Spektre Sep 06 '16 at 06:00
0
class Quo
{
    public static int QuotientBySuccessiveSubtraction(int m, int n)
    {
        int c=0;
        while(m>=n)
        {
            m-=n;
            ++c;
        }
        return c;
    }
    public static void main(int a, int b)
    {
        int q=QuotientBySuccessiveSubtraction(a,b);
        System.out.println("Quotient By Successive Subtraction= "+q);
    }
}
Dilip Meghwal
  • 632
  • 6
  • 15