C Exercises: Combinations of parentheses from n pairs
11. Generate Well-Formed Parentheses Variants
Write a C programming to generate all combinations of well-formed parentheses from n given pairs of parentheses.
Example: Input n = 5 Output: (()())(()) (()())()() (())((())) (())(()()) (())(())() (())()(()) (())()()() ()(((()))) ()((()())) ()((())()) ()((()))() ()(()(())) ()(()()()) ()(()())() ()(())(()) ()(())()() ()()((())) ()()(()()) ()()(())() ()()()(()) ()()()()()
C Code:
Sample Output:
((((())))) (((()()))) (((())())) (((()))()) (((())))() ((()(()))) ((()()())) ((()())()) ((()()))() ((())(())) ((())()()) ((())())() ((()))(()) ((()))()() (()((()))) (()(()())) (()(())()) (()(()))() (()()(())) (()()()()) (()()())() (()())(()) (()())()() (())((())) (())(()()) (())(())() (())()(()) (())()()() ()(((()))) ()((()())) ()((())()) ()((()))() ()(()(())) ()(()()()) ()(()())() ()(())(()) ()(())()() ()()((())) ()()(()()) ()()(())() ()()()(()) ()()()()()
Flowchart:

For more Practice: Solve these Related Problems:
- Write a C program to generate all combinations of well-formed parentheses for a given number of pairs using backtracking.
- Write a C program to count the total number of well-formed parentheses combinations for n pairs.
- Write a C program to generate well-formed parentheses and sort them lexicographically.
- Write a C program to generate well-formed parentheses combinations while limiting maximum nesting depth.
C Programming Code Editor:
Contribute your code and comments through Disqus.
Previous C Programming Exercise: Check if a given string is valid or not.
Next C Programming Exercise: Remove duplicates from an array of integers.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.