Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Need complete program as soon as possible please... Prompt the user and read the

ID: 3770187 • Letter: N

Question

Need complete program as soon as possible please...

Prompt the user and read the numbers a and b to be divided. Examine the signs of a and b to determine whether the result will be negative or positive. Then calculate the quotient a / b and the remainder using |a| and |b|, and print the quotient and remainder, with the appropriate signs. Put this in a loop so that the user can enter multiple pairs of numbers and perform division on them.

Implementing the Division

Set the quotient to 0.

Shift the divisor left until it is greater than the dividend, then shift it right 1 position. This lines up the divisor under the leading digit of the dividend. Keep track of how many positions you shifted; you will use this to control the division loop.

In the example shown, we "bring down" digits from the dividend until this intermediate dividend is greater than or equal to the divisor. Remember, in our code we don't "bring down" digits, instead we shift the divisor to the right at each step.

For each position you shifted to line up the divisor and dividend:

Shift the quotient left to add a zero to the end.

If the divisor > dividend, we can't do anything.

If divisor is <= dividend, this means that at this step we can subtract the divisor from the intermediate dividend and we get a 1 in the quotient. To put a 1 in the quotient, OR the quotient with 1. You have already shifted it, so you just need to change the low order digit from 0 to 1. Note that you could add 1, but OR is faster.

Shift the divisor to the right to prepare for the next step.

When you are done, the dividend is the remainder.

Explanation / Answer

#include <stdio.h>

int rem = 0;

int division(int dividend, int divisor)
{
int quotient = 1;

int neg = 1;
if ((dividend>0 &&divisor<0)||(dividend<0 && divisor>0))
neg = -1;

// Convert to positive
unsigned int tempdividend = (dividend < 0) ? -dividend : dividend;
unsigned int tempdivisor = (divisor < 0) ? -divisor : divisor;

if (tempdivisor == tempdividend) {
rem = 0;
return 1*neg;
}
else if (tempdividend < tempdivisor) {
if (dividend < 0)
rem = tempdividend*neg;
else
rem = tempdividend;
return 0;
}
while (tempdivisor<<1 <= tempdividend)
{
tempdivisor = tempdivisor << 1;
quotient = quotient << 1;
}

// Call division recursively
if(dividend < 0)
quotient = quotient*neg + division(-(tempdividend-tempdivisor), divisor);
else
quotient = quotient*neg + division(tempdividend-tempdivisor, divisor);
return quotient;
}


void main()
{
int dividend,divisor;
char ch = 'y';
while(ch == 'y')
{
printf (" Enter the Dividend: ");
scanf("%d", &dividend);
printf(" Enter the Divisor: ");
scanf("%d", &divisor);

printf(" %d / %d: quotient = %d", dividend, divisor, division(dividend, divisor));
printf(" %d / %d: rem = %d", dividend, divisor, rem);

printf(" Do you want to continue? (y for yes) ");
scanf(" %c",&ch);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote