Skip to content

Commit 6570e93

Browse files
Merge pull request #643 from rohitk2001/master
Divide Operator
2 parents 1e85ac8 + ec9e5a1 commit 6570e93

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

Divide Operator

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Python3 program to divide a number
2+
# by other without using / operator
3+
4+
# Function to find division without
5+
# using '/' operator
6+
def division(num1, num2):
7+
8+
if (num1 == 0): return 0
9+
if (num2 == 0): return INT_MAX
10+
11+
negResult = 0
12+
13+
# Handling negative numbers
14+
if (num1 < 0):
15+
num1 = - num1
16+
17+
if (num2 < 0):
18+
num2 = - num2
19+
else:
20+
negResult = true
21+
22+
elif (num2 < 0):
23+
num2 = - num2
24+
negResult = true
25+
26+
# if num1 is greater than equal to num2
27+
# subtract num2 from num1 and increase
28+
# quotient by one.
29+
quotient = 0
30+
31+
while (num1 >= num2):
32+
num1 = num1 - num2
33+
quotient += 1
34+
35+
# checking if neg equals to 1 then
36+
# making quotient negative
37+
if (negResult):
38+
quotient = - quotient
39+
return quotient
40+
41+
# Driver program
42+
num1 = 13; num2 = 2
43+
print(division(num1, num2))
44+
45+

0 commit comments

Comments
 (0)