How To Divide Two Number Without Divide / Operator


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int add(int a, int b)
{
  int carry=1,sum;
  while(carry!=0)
  {
     sum=a^b;
     carry=a&b;
     a=sum;
     b=carry<<1;



   }
   return sum;
}
int sub(int a, int b)
{
    int carry=1,sum;
    while(carry!=0)
    {
        sum=a^b;
        carry=(~a)&b;
        a=sum;
        b=carry<<1;


    }
    return sum;
}
int main()
{
    int n,m,i,f;
    scanf("%d %d",&n,&m);
    f=0;
    while(n>=m)
    {
       n=sub(n,m);
       f=add(f,1);
    }
    cout<<"Division="<<f<<endl;
    return 0;
}

Download Coding Interview Book and Get More Tutorials for Coding and Interview Solution: Click Here

Download System Design Interview Book and Get More Tutorials and Interview Solution: Click Here

Do you need more Guidance or Help? Then Book 1:1 Quick Call with Me: Click Here

Share on Google Plus

About Ashadullah Shawon

I am Ashadullah Shawon. I am a Software Engineer. I studied Computer Science and Engineering (CSE) at RUET. I Like To Share Knowledge. Learn More: Click Here
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment