program to calculate a Bodmas Expression like:-8/2(3-6)

+1 vote
asked Aug 2, 2019 by #question

1 Answer

0 votes
answered Aug 6, 2019 by (300 points)

In Python, you can do it using eval() function which evaluates an expression(given by user) using BODMAS rule.

x=eval(input("Enter an expression\n"))

In the terminal, enter expression as: -8/2*(3-6). Because, in any programming language you can not enter expression like this: -8/2(3-6). You need to put * before opening bracket.

This will result: 12.0

If you want result as 12, then use below expression:

x=int(eval(input("Enter an expression\n")))

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.