HeYa Brainlians and Programmers!!
Write a program to perform the following:
- The program asks user to input a mixed operation (Say, 2+3*6/5)
- The program then acts like a calculator and solve it and finally provide the output.
- If any other characters (other than numbers and special characters to be used) are used the program will throw an exception.
Language: Any (Java, Python preferred)
Good Luck, :)
Answers
Answered by
6
Hint: Use eval() for solving this problem.
I am writing the code in Python.
try:
exp=eval(input('Enter any expression: '))
print('Result after evaluation:',exp)
except:
print('An error occured.')
eval(): It is a built-in method which can be used to evaluate any expression.
General Syntax: eval(string)
Example:
print(eval('2+2'))
>> 4
x=5
print(eval('x*x'))
>> 25
#1
Enter any expression: 2+2
Result after evaluation: 4
#2
Enter any expression: 0/0
An error occured.
Attachments:
anindyaadhikari13:
Thanks for the brainliest :)
Similar questions