Write a program to accept two numbers and an operator and print the functions of a simple calculator.
Answers
Answered by
5
The given code is written in Python.
a=int(input('Enter a number: '))
b=int(input('Enter another number: '))
c=input("Enter an operator [+, -, *, /']: ")
if c in '+-*/%':
print('Result is:',eval('{}{}{}'.format(a,c,b)))
else:
print('Invalid Input.')
The problem is solved using eval() function. eval() returns the result after evaluation from the expression.
Consider the values -
> a = 5
> b = 6
> c = '+'
eval("{}{}{}".format(a,c,b))
= eval("5+6")
= 11 as sum of 5 and 6 is 11.
In this way, the problem is solved.
Attachments:
Similar questions