Question: write program in python language, and also no use of def, main void, import function just if-else-elif and while When you write mathematical expressions, as

write program in python language, and also no use of def, main void, import function just if-else-elif and while

When you write mathematical expressions, as well as lines of code in python, it is important to keep your parentheses balanced. For every open parenthesis, (, there must be a matching closed parenthesis, ). For instance, these math expressions have unbalanced parentheses:

  • 5 * (7 * (2 + 4)
  • ((7 / 2) + 5) - 3)

However, these have balanced parentheses:

  • (1 + 1) + (5 + 7 * 2)
  • 5 + (4 * (3 + (5 + 5)))

You should write a program that takes a string as input, and determines if the parentheses are balanced or not. You can use a while-loop to determine this. While looping through the characters, keep track of how many closed and open parentheses you see. If either of the below conditions is met, that indicated unbalanced parentheses:

  • If at any point in the loop there are more closed parentheses than open ones.
  • If after the loop, the number of open is not the same as the number of closed.

Some examples:

Enter string: 5 + (4 * (3 + (5 + 5))) Parentheses balanced 
Enter string: ()()(())) Parentheses unbalanced

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!