Skip to content
Home » Program that reads two numbers and an arithmetic operator and displays compound result in Python

Program that reads two numbers and an arithmetic operator and displays compound result in Python

Program to compute arithmetic operations in python

num_1=float(input("Enter first number:"))
num_2=float(input("Enter second number:"))
operator=input("Enter operator [+ - * / %]:")
result=0
if operator=='+':
    result=num_1+num_2
elif operator=='-':
    result=num_1-num_2
elif operator=='*':
    result=num_1*num_2
elif operator=='/':
    result=num_1/num_2
elif operator=='%':
    result=num_1%num_2
else:
    print("Invalid operator!!")
print(num_1, operator, num_2,'=',result)

Output 1:

Enter first number:15

Enter second number:6

Enter operator [+ – * / %]:+

15.0 + 6.0 = 21.0

Output 2:

Enter first number:7

Enter second number:45

Enter operator [+ – * / %]:-

7.0 – 45.0 = -38.0

Output 3:

Enter first number:13

Enter second number:6

Enter operator [+ – * / %]:*

13.0 * 6.0 = 78.0

Output 4:

Enter first number:45

Enter second number:5

Enter operator [+ – * / %]:/

45.0 / 5.0 = 9.0

Output 5:

Enter first number:56

Enter second number:5

Enter operator [+ – * / %]:%

56.0 % 5.0 = 1.0