one question though? why do you prefer division by bit operations, when there is / operator ( 32 bit div operation is inbuilt in almost all the processors so it can be done in constant time) also you recursion is not Tail-recursive so stack will grow like crazy!!
On Oct 10, 9:36 pm, harit agarwal <[email protected]> wrote: > traverse the array once and ge product of all elements , this is dividend > now again traverse for each a[i] do division(divisor,a[i]) > > code for division without using / > #include<stdio.h> > int dividend, divisor, remainder; > /* Division function Computes the quotient and remainder of two numbers > using bit shifting */ > int division(int tempdividend, int tempdivisor) { > int quotient = 1; > if (tempdivisor == tempdividend) { > remainder = 0; > return 1; > } else if (tempdividend < tempdivisor) { > remainder = tempdividend; > return 0; > } > while (tempdivisor <= tempdividend) { > /* Here divisor <> > divisor and quotient */ > tempdivisor = tempdivisor << 1; > quotient = quotient << 1; > } > /* We have reached the point where divisor > dividend, > therefore divide divisor and quotient by two */ > tempdivisor = tempdivisor >> 1; > quotient = quotient >> 1; > > /* Call division recursively for the difference to get the > exact quotient */ > quotient = quotient + division(tempdividend - tempdivisor, divisor); > > return quotient; > } > > /* Division of two numbers without using division operator */ > main() { > printf ("\nEnter the Dividend: "); > scanf("%d", ÷nd); > printf("\nEnter the Divisor: "); > scanf("%d", &divisor); > > printf("\n%d / %d: quotient = %d", dividend, divisor, > division(dividend, divisor)); > printf("\n%d / %d: remainder = %d", dividend, divisor, remainder); > } --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/algogeeks -~----------~----~----~----~------~----~------~--~---
