Python program to Add Two Numbers without Arithmetic operator

Posted by
  1. With for loop iteration with function
def add(a, b):
    for i in range(1, b + 1):
        a = a + 1
    return a

a = add(10, 32)
print(a)

2. Use bitwise operations to add two numbers.

def add_without_operator(a, b):
    while b != 0:
        data = a & b
        a = a ^ b
        b = data << 1
    return a
print(add_without_operator(2, 10))
print(add_without_operator(-20, 10))
print(add_without_operator(-10, -20))

Leave a Reply

Your email address will not be published. Required fields are marked *