Computer Science, asked by BrainlyProgrammer, 1 month ago

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 anindyaadhikari13
6

\textsf{\large{\underline{Solution}:}}

Hint: Use eval() for solving this problem.

I am writing the co‎de in Python.

try:

   exp=eval(input('Enter any expression: '))

   print('Result after evaluation:',exp)

except:

   print('An error occured.')

\textsf{\large{\underline{Explanation}:}}

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

\textsf{\large{\underline{Sample I/O}:}}

#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