-
Notifications
You must be signed in to change notification settings - Fork 12.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1929 from NitkarshChourasia/patch-1
Update Divide Operator.py
- Loading branch information
Showing
1 changed file
with
47 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |