CBSE Class 11 Computer Science
Question 88 of 91
String Manipulation — Question 12
Back to all questions 12
Question Question 12
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.
Solution
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