I have been recently given school work to make a recursive function that does division using only addition (no subtraction allowed) and has only 2 variables.
EDIT: A couple of notes based on the comments:
n1 is divided by n2. (n1:n2)
The answer should be a whole number (int) of how many times you can fit n2 inside n1 (8:3 should get 2, 8:4 should also get 2).
You can assume that the inputs are only whole positive numbers.
As asked in the comments, I will try my best to translate the assignment to English and make it as accurate as possible:
Write a recursive function named "PDiv" that gets two whole positive numbers and returns their whole quotient, using addition operations only.
I have tried to make it with 2 recursive functions like shown: (Assignment requires only one function, so it's not a right answer)
public static int PDiv(int n1, int n2)
{
if (n1 < n2)
return 0;
else if (n1 == n2)
return 1;
else
return PDiv(n1, n2 + n2, n2) + 1;
}
public static int PDiv(int n1, int n2, int con)
{
if (n1 < n2)
return 0;
else if (n1 == n2)
return 1;
else
return PDiv(n1, n2 + n2, con) + 1;
}
In addition to that, I have also tried that one which does work, but it's pretending to be wise while not really doing it with addition, but with the addition of a minus (basically subtraction). Example:
public static int PDiv(int n1, int n2)
{
if (n1 < n2)
return 0;
else if (n1 == n2)
return 1;
else
return PDiv(n1 + -n2, n2) + 1;
}
If anyone has an idea of how I can make it work, I would love to hear that! Thanks in advance!