In this tutorial, we will write a Python program to add, subtract, multiply and divide two input numbers.
Program to perform addition, subtraction, multiplication and division on two input numbers in Python
In this program, user is asked to input two numbers and the operator (+ for addition, – for subtraction, * for multiplication and / for division). Based on the input, program computes the result and displays it as output.
To understand this program you should know how to get the input from user and the basics of if..elif..else statement.
# Program published on https://beginnersbook.com # Python program to perform Addition Subtraction Multiplication # and Division of two numbers num1 = int(input("Enter First Number: ")) num2 = int(input("Enter Second Number: ")) print("Enter which operation would you like to perform?") ch = input("Enter any of these char for specific operation +,-,*,/: ") result = 0 if ch == '+': result = num1 + num2 elif ch == '-': result = num1 - num2 elif ch == '*': result = num1 * num2 elif ch == '/': result = num1 / num2 else: print("Input character is not recognized!") print(num1, ch , num2, ":", result)
Output 1: Addition
Enter First Number: 100 Enter Second Number: 5 Enter which operation would you like to perform? Enter any of these char for specific operation +,-,*,/: + 100 + 5 : 105
Output 2: Division
Enter First Number: 20 Enter Second Number: 5 Enter which operation would you like to perform? Enter any of these char for specific operation +,-,*,/: / 20 / 5 : 4.0
Output 3: Subtraction
Enter First Number: 8 Enter Second Number: 7 Enter which operation would you like to perform? Enter any of these char for specific operation +,-,*,/: - 8 - 7 : 1
Output 4: Multiplication
Enter First Number: 6 Enter Second Number: 8 Enter which operation would you like to perform? Enter any of these char for specific operation +,-,*,/: * 6 * 8 : 48
Related Python Examples:
1. Python program to add two matrices
2. Python program to add two binary numbers
3. Python program to add two numbers
4. Python program to swap two numbers
Leave a Reply