C/C++ How can I create a recursive function to mimic long division in C++?

  • Thread starter Thread starter FallArk
  • Start date Start date
  • Tags Tags
    C++ Recursion
AI Thread Summary
To create a recursive function in C++ that mimics long division, start by dividing the dividend by the divisor to get the integer part, then output that. The remainder is multiplied by 10 to continue the division process, and this is repeated recursively until the desired number of decimal places is reached. It's important to ensure the function either outputs the result directly or returns a string, rather than a double, to handle the infinite nature of the decimal. A suggested implementation includes a base case to stop recursion when the specified decimal places are exhausted.
FallArk
Messages
127
Reaction score
0
When I do things like
Code:
cout << 1/3;
in C++, it will typically output 0.333333, but the actual answer should be 0.3333333 with an infinite amount of 3s, how should I make a function such that will mimic this division recursively?
 
Technology news on Phys.org
FallArk said:
When I do things like
Code:
cout << 1/3;
in C++, it will typically output 0.333333, but the actual answer should be 0.3333333 with an infinite amount of 3s, how should I make a function such that will mimic this division recursively?

Hey FallArk! ;)

Do you know how to do long divisions?
That's the way to do it - with a recursive function to make it happen.

(Btw, your example code will output [m]0[/m] instead of [m]0.3333333[/m]. Know why?)
 
I like Serena said:
Hey FallArk! ;)

Do you know how to do long divisions?
That's the way to do it - with a recursive function to make it happen.

(Btw, your example code will output [m]0[/m] instead of [m]0.3333333[/m]. Know why?)

I know it's int so it outputs 0. Since that's the integer part. But would you mind explaining the long division and how it works? Thank you!
 
FallArk said:
I know it's int so it outputs 0. Since that's the integer part. But would you mind explaining the long division and how it works? Thank you!

Well... divide 1 by 3 as integers and output the result 0.
The remainder is 1.
Multiply the remainder by 10 and repeat.

Oh, and we need to output the decimal point after the first division.

More specifically:
\begin{array}{ccccc}
Dividend & Divisor & Quotient & Remainder & Output \\
1 & 3 & 0 & 1 & 0. \\
10 & 3 & 3 & 1 & 0.3 \\
10 & 3 & 3 & 1 & 0.33 \\
10 & 3 & 3 & 1 & 0.333 \\
...
\end{array}
 
I like Serena said:
Well... divide 1 by 3 as integers and output the result 0.
The remainder is 1.
Multiply the remainder by 10 and repeat.

Oh, and we need to output the decimal point after the first division.

More specifically:
\begin{array}{ccccc}
Dividend & Divisor & Quotient & Remainder & Output \\
1 & 3 & 0 & 1 & 0. \\
10 & 3 & 3 & 1 & 0.3 \\
10 & 3 & 3 & 1 & 0.33 \\
10 & 3 & 3 & 1 & 0.333 \\
...
\end{array}

If I want to output to, let's say, 10 decimal places. And I do a recursive function:
Code:
double longDivision (int dividend, int divisor, int decimalplaces){} // Are the data types correct?
What would be the general case when the function returns an output?
I want to say that when the amount of numbers after the decimal matches
Code:
int decimalplaces
,
is that correct?
 
FallArk said:
If I want to output to, let's say, 10 decimal places. And I do a recursive function:
Code:
double longDivision (int dividend, int divisor, int decimalplaces){} // Are the data types correct?
What would be the general case when the function returns an output?
I want to say that when the amount of numbers after the decimal matches
Code:
int decimalplaces
,
is that correct?

Looks fine to me.
Keep going! (Nod)

Oh, and the function shouldn't return a double.
Either it should return a string, or it should simply output the result to cout (and return void).
 
I like Serena said:
Looks fine to me.
Keep going! (Nod)

Oh, and the function shouldn't return a double.
Either it should return a string, or it should simply output the result to cout (and return void).

Cool! I will keep working on it!
 
Since making it print infinite decimal places would be pointless as that's the equivalent of an infinite loop why not just use the iomanip library or printf to output the number of decimal places you want to be output.
 
squidsk said:
Since making it print infinite decimal places would be pointless as that's the equivalent of an infinite loop why not just use the iomanip library or printf to output the number of decimal places you want to be output.

Because:
Code:
#include <stdio.h>
int main() {
    printf("%.20f\n", 1.0/3);
    return 0;
}
results in:
Code:
0.33333333333333331483
 
  • #10
I like Serena said:
Looks fine to me.
Keep going! (Nod)

Oh, and the function shouldn't return a double.
Either it should return a string, or it should simply output the result to cout (and return void).

I know how to do long division but how should I make it recursive?
Code:
void longDivision(int dividend, int divisor, int decimalplace){
    int x = dividend / divisor;
    cout << x;
    dividend = (dividend - (x * divisor)) * 10;
    cout << dividend / divisor;
}
 
  • #11
FallArk said:
I know how to do long division but how should I make it recursive?
Code:
void longDivision(int dividend, int divisor, int decimalplace){
    int x = dividend / divisor;
    cout << x;
    dividend = (dividend - (x * divisor)) * 10;
    cout << dividend / divisor;
}

Instead of the 2nd cout, we should have a call to longDivision(dividend, divisor, decimalPlaces -1).
Oh, and there should be an if to stop doing that when we have all the decimal places.
 
  • #12
I like Serena said:
Instead of the 2nd cout, we should have a call to longDivision(dividend, divisor, decimalPlaces -1).
Oh, and there should be an if to stop doing that when we have all the decimal places.

Duh! How did I not see that!
Thank you!
And here is my solution:
Code:
void longDivision(int dividend, int divisor, int decimalplace){
    if (decimalplace == 0) {
        return;
    }
    else {
        cout << dividend / divisor;
        longDivision((dividend % divisor) * 10, divisor, decimalplace - 1);
    }
}
:D
 
  • #13
You can clean up your solution by inverting the if statement as follows:

Code:
void longDivision(int dividend, int divisor, int decimalplace){
    if (decimalplace > 0) {
        cout << dividend / divisor;
        longDivision((dividend % divisor) * 10, divisor, decimalplace - 1);
    }
}

This eliminates the else and the empty return. In general it is better to have a single point of exit from a function.
 

Similar threads

Back
Top