Like Us Like Us Facebook Subscribe Subscribe us YouTube Whatsapp Share us Whatsapp Query Send your queries

Write a python program or script to take input for Two numbers and an Operator

Write a python program or script to take input for Two numbers and an Operator

Question : Write a python program or script to take input for Two numbers and an operator (+ , – , * , / ) and Based on the operator calculate and print the result? This question can be asked in CBSE Class XII Computer science with Python Paper.
Code : Please follow the code given below

# Write a python program to take input for Two numbers and
# an operator (+ , – , * , / ) and
# Based on the operator calculate and print the result?

x=int(input("Enter Number one : "))
y=int(input("Enter Number Two : "))
op=input("Enter Operator (+,-,*,/) : ")

if(op=="+"):
z=x+y
print("Sum = ",z)
elif(op=="*"):
z=x*y
print("Product = ",z)
elif(op=="-"):
if(x>y):
z=x-y
else:
z=y-x
print("Difference = ",z)
elif(op=="/"):
z=x/y
print("Division = ",z)
else:
print("Invalid operator")

Output :

Python script for calculation using operator

5 1 vote
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments