Computer Science

Write a program to input a formula with some brackets and checks, and prints out if the formula has the same number of opening and closing parentheses.

Python

Python String Manipulation

15 Likes

Answer

str = input("Enter a formula: ")
count = 0

for ch in str :
    if ch == '(' :
        count += 1
    elif ch == ')' :
        count -= 1

if count == 0 :
    print("Formula has same number of opening and closing parentheses")
else :
    print("Formula has unequal number of opening and closing parentheses")

Output

Enter a formula: s(s-a)(s-b)(s-c)
Formula has same number of opening and closing parentheses

=====================================

Enter a formula: s((s-a)(s-b)(s-c)
Formula has unequal number of opening and closing parentheses

Answered By

9 Likes


Related Questions