Given a polynomial string **str**, the task is to integrate the given string and print the string after integrating it.

**Note:** The input format is such that there is a whitespace between a term and the ‘+’ symbol.

**Examples:**

Input:str = “4X^{3}+ 3X^{1}+ 2X^{2}”Output:X^{4}+ (3/2)X^{2}+ (2/3)X^{3}+ C

Input:str = “5X^{3}+ 7X^{1}+ 2X^{2}+ 1X^{0}”Output:(5/4)X^{4}+ (7/2)X^{2}+ (2/3)X^{3}+ X^{q}+ C

**Approach:** The idea is to observe that when the given equation consists of multiple polynomials , the integration of the given polynomial .

Also it is known that the indefinite integral of is .

Therefore, we split the given string and integrate every term in it.

Below is the implementation of the above approach:

`// C++ program to find the indefinite` `// integral of the given polynomial` ` ` `#include "bits/stdc++.h"` `#define MOD (1e9 + 7);` `using` `ll = int64_t;` `using` `ull = uint64_t;` `#define ll long long` `using` `namespace` `std;` ` ` `// Function to perform the integral` `// of each term` `string inteTerm(string pTerm)` `{` ` ` `// Get the coefficient` ` ` `string coeffStr = ` `""` `, S = ` `""` `;` ` ` `int` `i;` ` ` ` ` `// Loop to iterate and get the` ` ` `// Coefficient` ` ` `for` `(i = 0; pTerm[i] != ` `'x'` `; i++)` ` ` `coeffStr.push_back(pTerm[i]);` ` ` `long` `long` `coeff` ` ` `= ` `atol` `(coeffStr.c_str());` ` ` ` ` `string powStr = ` `""` `;` ` ` ` ` `// Loop to find the power` ` ` `// of the term` ` ` `for` `(i = i + 2; i != pTerm.size(); i++)` ` ` `powStr.push_back(pTerm[i]);` ` ` ` ` `long` `long` `power` ` ` `= ` `atol` `(powStr.c_str());` ` ` `string a, b;` ` ` `ostringstream str1, str2;` ` ` ` ` `// For ax^n, we find a*x^(n+1)/(n+1)` ` ` `str1 << coeff;` ` ` `a = str1.str();` ` ` `power++;` ` ` `str2 << power;` ` ` `b = str2.str();` ` ` `S += ` `"("` `+ a + ` `"/"` `+ b + ` `")X^"` `+ b;` ` ` ` ` `return` `S;` `}` ` ` `// Function to find the indefinite` `// integral of the given polynomial` `string integrationVal(string& poly)` `{` ` ` ` ` `// We use istringstream to get the` ` ` `// input in tokens` ` ` `istringstream is(poly);` ` ` ` ` `string pTerm, S = ` `""` `;` ` ` ` ` `// Loop to iterate through` ` ` `// every term` ` ` `while` `(is >> pTerm) {` ` ` ` ` `// If the token = '+' then` ` ` `// continue with the string` ` ` `if` `(pTerm == ` `"+"` `) {` ` ` `S += ` `" + "` `;` ` ` `continue` `;` ` ` `}` ` ` ` ` `if` `(pTerm == ` `"-"` `) {` ` ` `S += ` `" - "` `;` ` ` `continue` `;` ` ` `}` ` ` ` ` `// Otherwise find` ` ` `// the integration of` ` ` `// that particular term` ` ` `else` ` ` `S += inteTerm(pTerm);` ` ` `}` ` ` `return` `S;` `}` ` ` `// Driver code` `int` `main()` `{` ` ` `string str` ` ` `= ` `"5x^3 + 7x^1 + 2x^2 + 1x^0"` `;` ` ` `cout << integrationVal(str)` ` ` `<< ` `" + C "` `;` ` ` `return` `0;` `}` |

**Output:**

(5/4)X^4 + (7/2)X^2 + (2/3)X^3 + (1/1)X^1 + C

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the **Essential Maths for CP Course** at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**