Skip to content

Commit

Permalink
Merge pull request #1929 from NitkarshChourasia/patch-1
Browse files Browse the repository at this point in the history
Update Divide Operator.py
  • Loading branch information
geekcomputers authored Jul 21, 2023
2 parents e0a7182 + 5c3df29 commit 5929c11
Showing 1 changed file with 47 additions and 44 deletions.
91 changes: 47 additions & 44 deletions Divide Operator.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,49 @@
# Python3 program to divide a number
# by other without using / operator

# Function to find division without
# using '/' operator
def division(num1, num2):

if (num1 == 0): return 0
if (num2 == 0): return INT_MAX

negResult = 0

# Handling negative numbers
if (num1 < 0):
num1 = - num1

if (num2 < 0):
num2 = - num2
else:
negResult = true
# If num2 is negative, make it positive
elif (num2 < 0):
num2 = - num2
negResult = true

# if num1 is greater than equal to num2
# subtract num2 from num1 and increase
# quotient by one.
quotient = 0

while (num1 >= num2):
num1 = num1 - num2
quotient += 1

# checking if neg equals to 1 then
# making quotient negative
if (negResult):
quotient = - quotient
return quotient

# Driver program
num1 = 13; num2 = 2
# Pass num1, num2 as arguments to function division
print(division(num1, num2))
class DivisionOperation:
INT_MAX = float('inf')

def __init__(self, num1, num2):
self.num1 = num1
self.num2 = num2

def perform_division(self):
if self.num1 == 0:
return 0
if self.num2 == 0:
return self.INT_MAX

neg_result = False

# Handling negative numbers
if self.num1 < 0:
self.num1 = -self.num1

if self.num2 < 0:
self.num2 = -self.num2
else:
neg_result = True
elif self.num2 < 0:
self.num2 = -self.num2
neg_result = True

quotient = 0

while self.num1 >= self.num2:
self.num1 -= self.num2
quotient += 1

if neg_result:
quotient = -quotient
return quotient


# Driver program
num1 = 13
num2 = 2

# Create a DivisionOperation object and pass num1, num2 as arguments
division_op = DivisionOperation(num1, num2)

# Call the perform_division method of the DivisionOperation object
result = division_op.perform_division()

print(result)

0 comments on commit 5929c11

Please sign in to comment.