Write a Python program to add two positive integers without using the '+' operator


Introduction

Write a Python program to add two positive integers without using the '+' operator. I have used python 3.7 compiler for debugging purpose.


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1. def add_without_plus_operator(x, y):
  2. while y != 0:
  3. data = x & y
  4. x = x ^ y
  5. y = data << 1
  6. return x
  7. print(add_without_plus_operator(2, 10))
  8. print(add_without_plus_operator(-20, 10))
  9. print(add_without_plus_operator(-10, -20))

Result

python language techstudy.org

     





Related Topics




Popular Posts