# 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).

Expert's answer

/*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;

}

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;

}

## Comments

## Leave a comment