Welcome~~~


Another blog:
http://fun-st.blogspot.com/

It is easier to write an incorrect program than understand a correct one.

Wednesday, February 9, 2011

# Divide two numbers without using division operator

Suppose two numbers be 'a' and 'b' and we want to find a/b

int quotient(int a,int b){
  int t = 1 ;
  while(b*t <= a){
     t++;
  }
  return t -1 ;
}


♥ ¸¸.•*¨*•♫♪♪♫•*¨*•.¸¸♥
http://www.cracktheinterview.net/amazon/525-divide-two-numbers-without-using-division-operator.html

1 comment:

  1. int div(int a, int b)
    {
    if(!b) return -1;
    if(a<b) {
    printf("%d / %d = %d , remainder %d\n", a, b, 0, a);
    return 0;
    }
    int q = 1, t=d, d=1;
    while (t<a)
    {
    d=t;
    t=t<<2;
    q=q<<1;
    }
    while (d+b<a)
    {
    d=d+b;
    ++q;
    }
    printf("%d / %d = %d , remainder %d\n", a, b, q, a-d);
    return 0;
    }

    ReplyDelete