codescracker


python

Python Program Addition Subtraction Multiplication Division



« Previous Program Next Program »


Addition Subtraction Multiplication Division in Python

To perform addition, subtraction, multiplication and division in python, you have to ask from user to enter any two number, and then ask again to enter the operator to perform the desired operation. After performing the desired operation, print the result as output. You may also like to get the code of calculator program in python to perform the same action in perfect way.

Python Programming Code to Perform Addition Subtraction Multiplication Division

Following python program ask from user to enter the two number and then ask to enter the operator to perform the operation and display the result as output:

# Python Program - Addition Subtraction Multiplication Division
		
while True:
	print("Enter 'x' for exit.")
	print("Enter any two number: ")
	num1 = input()
	num2 = input()
	if num1 == 'x':
		break
	else:
		ch = input("Enter operator (+,-,*,/): ")
		if ch == '+':
			res = int(num1) + int(num2)
			print(num1, "+", num2, "=", res)
		elif ch == '-':
			res = int(num1) - int(num2)
			print(num1, "-", num2, "=", res)
		elif ch == '*':
			res = int(num1) * int(num2)
			print(num1, "*", num2, "=", res)
		elif ch == '/':
			res = int(num1) / int(num2)
			print(num1, "/", num2, "=", res)
		else:
			print("Strange input")

Here are the sample runs of the above python program to illustrate how to perform addition, subtraction, multiplication, and division in python. Here is the first snapshot of sample run of above python program:

python addition subtraction multiplication division

This is the second snapshot of sample run of above python program:

addition subtraction multiplication and division

Same Program in Other Languages

You may also like to learn or practice the same program in other popular programming languages:


« Previous Program Next Program »




Tools
Calculator

Quick Links
Signup - Login - Give Online Test