# C Program for Balanced Parenthesis

Today we are going to learn about balancing the parenthesis in a block of code or in any expression. C Program for Balanced Parenthesis especially helpful when we need to check for opening and closing of a code block. Because, if a code block has non balanced brackets or parenthesis, the compiler will throw errors.

Without much delay, let’s get our coding brains to use and get started!

## Parenthesis: An Overview:

Parenthesis are a set of symbols used in mathematical expressions.We define blocks of code and statements in a program using parenthesis. The symbols of parenthesis are:

1. () : Round braces which is used to define functions
2. [] : Square braces which is used to define arrays
3. {} : Curly braces used to define code blocks like opening and closing of function blocks, if blocks, etc.
4. <> : Angular braces used for comparisons and defining headers.

Apart from their respective uses in coding, parentheses have their own place when it comes to mathematical expressions. Usually, we use them to maintain an order in an expression.

For example, (1+[{2+5}/3])

We solve the inner braces followed by the arithmetic hierarchy i.e. the BODMAS rule.

## What is the need to balance Parenthesis?

As discussed earlier, parentheses serve an important purpose to both programming and arithmetic expressions.

They are responsible for defining a block of codes and establishing a mathematical operations hierarchy. Balancing the parenthesis will ensure the smooth running of a group of statements and will also provide accurate results of a mathematical equation or expression.

### Example to understand better:

Let us take an example of a mathematical expression given before, i.e. (1+[{2+5}/3]).

The correct result for the above expression is 3.33333…. But if we remove one parenthesis, there will be discrepancy in the result.

Let’s say we write the expression with a missing closing curly brace, i.e. (1+[{2+5/3]), this will generate an error or result in an inaccurate solution. As it confuses the compiler.

In the same way if we take a code block :

if(a>1)

{

S++;

And we forget the closing braces, there will be a compilation error for the non proper closure of code block. This will result in havoc in your entire code and if you have included multiple code blocks, it will be even more difficult to remove these errors.

While handling large files containing hundreds and thousands of lines of code, it is highly likely that the programmer will miss a pair of parenthesis. Thus, there is a dis-balance in the number of parenthesis in the program file.

If these files having irregular amount of parenthesis are run, they will generate errors and the programmer will find difficult to locate due to the bulk of codes.

Hence, writing a program for checking the balancing of parenthesis will help you spot the error by generating a statement.

This program will help us detect the expression or a coding block which has non equal number of parentheses. Therefore, learning to code this particular program will be helpful for managing large files of codes.

## Algorithm:

The algorithm for reaching a solution of this problem which is C Program for Balanced Parenthesis is really simple. Therefore, understanding the algorithm will boost your clarity and finally when you will read the code, you will be able to decipher it easily.

Again, the rule of coding is simple. Go through the algorithm, solve the code by yourself before looking into the solution.

The algorithm below is easily understandable and with a little devotion of time I’m sure you will be able to code the program for yourself easily. In case you still are not able to solve it, you can go through the program provided below.

You can view and go through the algorithm followed for the c program for checking the balance of parenthesis:

1. Start
2. Define an expression variable for the user to enter
3. Declare x and i
4. Initialize x= 0 and i = 0
5. Read the expression entered by the user
1. If it is open parenthesis, increment x by 1
2. If it is a close parenthesis, decrement x by 1
6. Increment i
7. If x is 0, expression has balanced parentheses
8. else it is unbalanced
9. Print the result
10. Stop

## Program:

You can read the C Program for Balanced Parenthesis below:

``````// C program to check the balanced parenthesis.
#include<stdio.h>
int main()
{
char expression; // declaration of char type array
int x=0, i=0; // declaration of two integer type variables
printf("\nEnter an expression");
scanf("%s", expression);
// Scanning the expression until we reach the end
while(expression[i]!= '\0')
{
//to check the symbol is '('
if(expression[i]=='(')
{
x++; // incrementing 'x' variable
}
// condition to check the symbol is ')'
else if(expression[i]==')')
{
x--; // decrementing 'x' variable
if(x<0)
break;
}
i++; // incrementing 'i' variable.
}
// Condition to check whether x is equal to 0 or not.
if(x==0)
{
printf("Expression is balanced");
}
else
{
printf("Expression is unbalanced");
}
return 0;
}``````

## Output:

There can be two possible outputs for the above line of codes.

Output for a unbalanced expression:

Output for a balanced expression:

Note: We can also solve the program using stacks. Judging by the parenthesis that will remain at the end of the stack, we can determine if the expression is unbalanced or not.

## Program Explanation:

What we are really doing in the program is just reading the expression entered by the user, traversing each symbol and checking if it has an equal number of open and close parenthesis.

1. First, we have given the user the freedom to enter and define their own expression.
2. Then we have defined a while loop which will go on till the end of expression, i.e. it will traverse through each and every symbol present in the expression.
3. Next comes the if statements. These if statements check for the number of opening and closing parenthesis.
4. We change the value of x, i.e. increment or decrement according to the checking conditions in if block.
5. Once we obtain the final value of x, we can check for the balance.
6. If it is 0, that implies there are equal numbers of open and close parenthesis. Anything other than 0 means the number of open parenthesis is not equal to the number of closed parenthesis. Hence, we see an unbalanced expression.
7. We finally display the result.

So, this is how we complete the C Program for Balanced Parenthesis

## Wrapping it up:

Balancing the parenthesis in an expression can come in handy when it comes to large files. Using this code of C Program for Balanced Parenthesis, we can also check for other type of parenthesis such as curly, square, etc. This easy to implement program makes it easy to reduce the amount of time you will need to go through bulk of codes and check if the parentheses are balanced to avoid errors.

So what are you waiting for? Modify the code as you want to fit into other purposes. Looking forward to your feedback. See you in the next post!