Algorithm for Addition of Two Numbers in Python

We must understand the basic prerequisites of Python before understanding the algorithm for adding two numbers in Python. This includes:

  • Basic Input and Output
  • Operators
  • Data Types

Input: We take input from users while programming or performing any operation. We take input using the input( ) function.

Syntax:

Output: The output is the result after the operations performed on the inputs. We use the print( ) function to display the output.

Syntax:

value is the output to be printed.

Data Types: Data types are the type of data which is stored in a variable. There are different data types in Python, including int, float, str, list, tuple, etc.

Operators: The operators are the symbols used to perform operations on the input variables. There are different types of operators in Python, including arithmetic, assignment, comparison, logical operators, etc.

For adding two numbers, we will use the + operator

Now, let's see a simple algorithm to add two numbers in Python.

Algorithm:

  1. Declare two variables, n1 and
  2. Assign values to both the variables.
  3. Add the numbers using the + operator.
  4. Print the output.

Example:

Code:

Output:

Sum : 109

Explanation:

We take two variables, n1 and n2, and assign values to them. Then, using the + operator, we print the sum of both numbers.

Adding Two Numbers with User Input

We will take the inputs from the user, add the numbers and print the output.

Algorithm:

  1. Declare two variables.
  2. Take inputs from the user using the input( ) function
  3. Add the numbers using the + operator.
  4. Print the output.

Example:

Code:

Output:

Enter number 1 : 233
Enter number 2 : 112
Sum :  345

Explanation:

We have declared two variables, n1 and n2. Using the input( ) function, we take from the user. Then, we added the numbers using the + operator and stored the value in another variable, Sum. With the help of the print( ) function, we printed the output.

Adding two numbers by defining a function

We will define a function for adding the numbers and returning the output.

Algorithm:

  1. Define a function having two numbers as its parameters.
  2. Using a third variable, add the numbers using the + operator
  3. Call an object of the function which takes 2 numbers
  4. Print the output

Example:

Code:

Output:

Sum :  187

Explanation:

We made a function sum_num( ) with parameters n1 and n2. Then, using a third variable, we added both the numbers and then printed the output. Then, we made an object to call the function, which takes the values and prints the sum of the numbers.

Adding two numbers using the add( ) method

We will use the add( ) method of the operator library to add two numbers.

Algorithm:

  1. Import the operator library.
  2. Initialise two variables with the numerical value.
  3. Using a third variable, find the sum of numbers using the operator.add( ) method.
  4. Print the output.

Example:

Code:

Output:

Sum :  59

Explanation:

We have imported the operator library. Then, initialised two variables and added the numbers using the operator.add( ) method. Then, we printed the output.