Answer to Question #4231 in C++ for shaniemae

Question #4231
A division of one positive integer number by another positive integer number is often performed in elementary schools as repeated subtraction. input two positive integer number ,x and y.Then compute and display the quotient q and the remainder r of x divided y without using any division operator or function(slash operator/modulo operator).
1
Expert's answer
2011-10-14T11:12:24-0400
/*I used a recursive function to calculate the quotient and the
remainder.*/

#include<stdio.h>
#include<iostream>
using
namespace std;
int x, y, r;

int division(int tempdividend, int
tempdivisor) {
int q = 1;
if (tempdivisor == tempdividend) {
r =
0;
return 1;
} else if (tempdividend < tempdivisor) {
r =
tempdividend;
return 0;
}

while (tempdivisor <= tempdividend)
{
tempdivisor = tempdivisor << 1;
q = q <<
1;
}
tempdivisor = tempdivisor >> 1;
q = q >> 1;
q = q +
division(tempdividend - tempdivisor, y);

return q;
}

int main()
{
int q = 0;
cout << "Enter x: ";
cin >> x;
cout
<< "Enter y: ";
cin >> y;
q = division(x, y);
cout <<
"( x divided y)" << endl;
cout << " q = " << q <<
endl;
cout << " r = " << r << endl;
return
0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS