Python Program to Add Two Numbers Without Using the “+” Operator
In the previous article, we have discussed Python Program to Find out the Arc Length of an Angle
In order to add two numbers without using the ‘+’ operator, we should use a mathematical formula.
Formula :
Let x, y are the given two numbers, then
x+y = x^2-y^2 = (x+y)(x-y) if (x!=y).
x+y = 2*x ( if x=y )
Given two numbers and the task is to add the given two numbers without using the ‘+’ operator.
Examples:
Example 1:
Input:
Given First Number = 65 Given Second Number = 45
Output:
The sum of given two numbers = 110.0
Example 2:
Input:
Given First Number = 220.5 Given Second Number = 100
Output:
The sum of given two numbers = 320.5
Program to Add Two Numbers Without Using the “+” Operator.
Below are the ways to add the given two numbers without using the ‘+’ operator.
Method #1: Using Mathematical Formula (Static Input)
Approach:
- Give the first number as static input and store it in a variable.
- Give the second number as static input and store it in another variable.
- Check if the first number is not equal to the second number using the if conditional statement.
- If the statement is true, then print the value of (fst_num*fst_num-sec_num*sec_num)/(fst_num-sec_num)
- Else print the value of (2*fst_num).
- The Exit of the program.
Below is the implementation:
# Give the first number as static input and store it in a variable. fst_num = 2.5 # Give the second number as static input and store it in another variable. sec_num = 8 # Check if the first number is not equal to the second number using the if conditional # statement. if fst_num != sec_num: # If the statement is true, then print the value of # (fst_num*fst_num-sec_num*sec_num)/(fst_num-sec_num) print("The sum of given two numbers =", (fst_num*fst_num-sec_num*sec_num)/(fst_num-sec_num)) else: # Else print the value of (2*fst_num). print("The sum of given two numbers =", 2*fst_num)
Output:
The sum of given two numbers = 10.5
Method #2: Using Mathematical Formula (User Input)
Approach:
- Give the first number as user input using the float(input()) function and store it in a variable.
- Give the second number as user input using the float(input()) function and store it in another variable.
- Check if the first number is not equal to the second number using the if conditional statement.
- If the statement is true, then print the value of (fst_num*fst_num-sec_num*sec_num)/(fst_num-sec_num)
- Else print the value of (2*fst_num).
- The Exit of the program.
Below is the implementation:
# Give the first number as user input using the float(input()) function and #store it in a variable. fst_num = float(input("Enter some random number = ")) # Give the second number as user input using the float(input()) function and #store it in another variable. sec_num = float(input("Enter some random number = ")) # Check if the first number is not equal to the second number using the if conditional # statement. if fst_num != sec_num: # If the statement is true, then print the value of # (fst_num*fst_num-sec_num*sec_num)/(fst_num-sec_num) print("The sum of given two numbers =", (fst_num*fst_num-sec_num*sec_num)/(fst_num-sec_num)) else: # Else print the value of (2*fst_num). print("The sum of given two numbers =", 2*fst_num)
Output:
Enter some random number = 50 Enter some random number = 70 The sum of given two numbers = 120.0
Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.
Python Program to Add Two Numbers Without Using the “+” Operator Read More »