0
Good night, you guys!
I am performing an activity whose goal is to use a recursive function to perform a whole division only through successive subtractions.
I made a while loop and only it (which I ran in a piece of code) worked. The problem is using it in the recursive function. I was able to compile it, but the result is not correct.
Looking at the code I still haven’t found the problem (I thought it was the re-turn i in function, but the result has not changed, or maybe in the declaration of variables - but I still can not say)... I started learning recursive functions recently, in case anyone can help, thank you much the help!
Tie that works itself:
#include <stdio.h>
int main (){
int a, b, i = 0;
printf ("Digite o dividendo (numero que será dividido):");
scanf ("%d", &a);
printf ("Digite o divisor:");
scanf ("%d", &b);
while (a > 0 && a >= b){
a = a - b;
i++;
}
printf ("O resultado da divisao e: %d", i);
return 0;
}
Recursive function that goes wrong:
#include <stdio.h>
int main (){
int a,b;
printf ("Digite o dividendo (numero que será dividido): ");
scanf ("%d", &a);
printf ("Digite o divisor: ");
scanf ("%d", &b);
printf ("O resultado da divisao e: %d. \n", divide(a,b));
return 0;
}
int divide (a,b){
int i = 0;
while (a > 0 && a >= b){
a = a - b;
i++;
return i;
}
}
Hi Bruno, I appreciate the answer! However if I use the Return divide(a, b), the result still remains the same as before... I also realized that most of the time the value of i ends up being equal to the value of b (I don’t know if he ended up doing the wrong calculation or if there was some conflict between the variables).
– Luana