Minus in python: In the previous article, we have discussed Python Program to Multiply Two Numbers Without Using Multiplication(*) Operator
Given two numbers and the task is to subtract the given two numbers without using the minus(*) Operator in python.
Two’s Complement:
How to subtract variables in python: The negative of a binary integer, which is obtained by changing all ones to zeros and then adding one to the result.
Examples:
Example1:
Input:
Given First Number = 1500 Given Second Number = 700
Output:
The Subtraction of above given two numbers{ 1500 - 700 } = 800
Example2:
Input:
Given First Number = 400 Given Second Number = 200
Output:
The Subtraction of above given two numbers{ 400 - 200 } = 200
- Python Program to Make a Simple Calculator
- Python Program to Read Two Numbers and Print Their Quotient and Remainder
- Python Program to Calculate the HCF/GCD
Program to Subtract Two Numbers Without Using Minus(-) Operator in Python
How to subtract in python: Below are the ways to subtract the given two numbers without using the minus(*) Operator in python:
Method #1: Using 2’s Complement Method (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.
- Calculate the two’s complement of the given second number using the complement (~) operator.
- Store it in another variable.
- Add the above result to the given first number and store it in another variable.
- Print the subtraction of the given two numbers without using the minus(*) Operator.
- The Exit of the Program.
Below is the implementation:
# Give the first number as static input and store it in a variable. fst_numb = 1500 # Give the second number as static input and store it in another variable. scnd_numb = 700 # Calculate the two's complement of the given second number using the complement (~) # operator. # Store it in another variable. twos_complemnt = (~scnd_numb+1) # Add the above result to the given first number and store it in another variable. rslt_subtctn = fst_numb+twos_complemnt # Print the subtraction of the given two numbers without using the minus(*) Operator. print("The Subtraction of above given two numbers{", fst_numb, "-", scnd_numb, "} =", rslt_subtctn)
Output:
The Subtraction of above given two numbers{ 1500 - 700 } = 800
Method #2: Using 2’s Complement Method (User Input)
Approach:
- Give the first number as user input using the int(input()) function and store it in a variable.
- Give the second number as user input using the int(input()) function and store it in another variable.
- Calculate the two’s complement of the given second number using the complement (~) operator.
- Store it in another variable.
- Add the above result to the given first number and store it in another variable.
- Print the subtraction of the given two numbers without using the minus(*) Operator.
- The Exit of the Program.
Below is the implementation:
# Give the first number as user input using the int(input()) function and # store it in a variable. fst_numb = int(input("Enter some random number = ")) # Give the second number as user input using the int(input()) function and # store it in another variable. scnd_numb = int(input("Enter some random number = ")) # Calculate the two's complement of the given second number using the complement (~) # operator. # Store it in another variable. twos_complemnt = (~scnd_numb+1) # Add the above result to the given first number and store it in another variable. rslt_subtctn = fst_numb+twos_complemnt # Print the subtraction of the given two numbers without using the minus(*) Operator. print("The Subtraction of above given two numbers{", fst_numb, "-", scnd_numb, "} =", rslt_subtctn)
Output:
Enter some random number = 600 Enter some random number = 100 The Subtraction of above given two numbers{ 600 - 100 } = 500
Remediate your knowledge gap by attempting the Python Code Examples regularly and understand the areas of need and work on them.