Python program to add two numbers

PythonServer Side ProgrammingProgramming

In this article, we will learn about the solution and approach to solve the given problem statement.

Problem statement

We will be given two large numbers and we need to add them and display the output.

The bruteforce approach will be using the “+” operator between the operands or we can store two numbers in an iterable and use the inbuilt sum function available in Python standard library.

In this approach, time complexity is increased as the computation takes place on decimal numbers directly.

Now lets’s discuss another approach that involves working on the bits of the decimal numbers.

Here we will use the concept of adders that computes the sum and the carry .

Now let’s see the implementation −


 Live Demo

def Add(x, y):
   # carry becomes null
   while (y != 0):
      # carry with common bits
      carry = x & y
      # Sum of bits of x and y
      x = x ^ y
      # Carry is shifted by one
      y = carry << 1
   return x
print(Add(19, 34))



All variables and functions are declared in the global scope as shown in the figure below.


In this article, we learned about the approach to add two numbers by the help of a full adder.

Published on 26-Sep-2019 12:06:43