We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 80ee47d commit d7c2b53Copy full SHA for d7c2b53
BitManipulation/divide_integers/divide_integers.cpp
@@ -0,0 +1,33 @@
1
+//Divide two integers without using multiplication, division and mod operator. Print floor of division
2
+
3
+#include<iostream>
4
5
+using namespace std;
6
7
+int divide(int A, int B) {
8
+ long long int ans=0,sign;
9
+ if(B==1||B==-1){
10
+ if(B==1)
11
+ return min(INT_MAX,A);
12
+ else{
13
+ long long int x=(long long int)A*(-1);
14
+ return min((long long int)INT_MAX,x);
15
+ }
16
17
+ sign=((A<0)^(B<0))?-1:1;
18
+ A=abs(A);
19
+ B=abs(B);
20
+ while(A>=B){
21
+ A-=B;
22
+ ans++;
23
24
+ return (int)(ans*sign);
25
+}
26
27
+int main()
28
+{
29
+ int a,b,x;
30
+ scanf("%d %d",&a,&b);
31
+ printf("Division is %d\n",divide(a,b));
32
+ return 0;
33
0 commit comments