Related Articles
Print all the possible arithmetic expressions for a given number
• Last Updated : 28 Apr, 2020

Given an integer N, the task is to print all the possible arithmetic expressions using all numbers from 1 to N and with binary operator +, –, * and /.

Examples:

Input: n = 2
Output:
1+2, 1-2, 1/2, 1*2

Input: n = 3
Output:
1+2+3, 1+2-3, 1+2/3, 1+2*3, 1-2+3, 1-2-3, 1-2/3, 1-2*3
1/2+3, 1/2-3, 1/2/3, 1/2*3, 1*2+3, 1*2-3, 1*2/3, 1*2*3

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

• We will create a character array of length = n + n – 1, because for an expression with n operands to be valid we will need n-1 operators.
• Iterate the array and put numbers at even position whereas symbols at the odd position and call the function recursively.
• If number of characters becomes equal to the length of array, print the array.

Below is the implementation of the above approach:

 `// C++ program to print all the ``// expressions for the input value `` ` `#include``#include``using` `namespace` `std;`` ` `// Function to print all the``// expressions using the number``void` `PrintRecursive(``char` `*str,``int` `arr[],``                 ``int` `i, ``int` `n,``char` `*res,``                 ``int` `j, ``int` `len,``int` `ln)``{``    ``// Termination condition``    ``if``(j==len)``    ``{``        ``res[j]=``'\0'``;``        ``cout<
Output:
```1+2
1-2
1/2
1*2
```

My Personal Notes arrow_drop_up