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

print("Enter 'x' for exit.");
print("Enter any two number: ");
num1 = input();
num2 = input();
if num1 == 'x':
    exit();
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..exiting..");

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 asking from user to enter any two number:

python add subtract multiply divide

Now let's type any two numbers say 10 and 20 and then check it out for addition operation in python by typing + operator and then press ENTER key to find the output as shown in the given sample output here:

addition subtractoin division multiplication python

Now let's re-run the same program and this time check it out for subtraction operation as shown here in the following screenshot. Here i am going to enter the two number, that is, first number is 20 and second number is 10 and - is the operand then press enter key.

subtraction python

Now let's check the same program for multiplication operation as shown here with 10 and 20

multiplication python

Again re-run the same program to check for division operation as shown here in the below sample output with the same two numbers as used in multiplication operation:

division python

Here is the same program written and run on python shell. This is the first screenshot:

python addition subtraction multiplication division

This is the second screenshot:

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