Given a polynomial string str, the task is to differentiate the given string and print the string after differentiating it.
Note: The input format is such that there is a whitespace between a term and the ‘+’, ’-’ symbol
Examples:
Input: str = “4X3 + 3X1 + 2X2”
Output: “12X2 + 3X0 + 4X1”
Explanation:
The derivative of p(x) = A*XN is p'(x) = A * N * XN – 1Input: str = “5X4 + 6X2 + 5X2”
Output: “20X3 + 12X1 + 10X1”
Approach: The idea is to observe that when the given equation consists of multiple polynomials
, the differentiation of the given polynomial
.
And, it is known that the derivative of
is ![]()
Therefore, we split the given string and differentiate every term in it.
Below is the implementation of the above approach:
// C++ program to differentiate 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 differentiate the // given term string diffTerm(string pTerm) { // Get the coefficient string coeffStr = "", S = ""; int i; // Loop to get the coefficient for (i = 0; pTerm[i] != 'x'; i++) coeffStr.push_back(pTerm[i]); long long coeff = atol(coeffStr.c_str()); // Loop to get the power of each term string powStr = ""; for (i = i + 2; i != pTerm.size(); i++) powStr.push_back(pTerm[i]); long long power = atol(powStr.c_str()); string a, b; // Converting the value // to the string ostringstream str1, str2; // For ax^n, we find (n)*a*x^(n-1) coeff = coeff * power; str1 << coeff; a = str1.str(); power--; str2 << power; b = str2.str(); S += a + "X^" + b; return S; } // Function to differentiate the // given polynomial string diffstr(string& poly) { // We use istringstream to get // the input in tokens istringstream is(poly); string pTerm, S = ""; // For every token, compute the // differentiation while (is >> pTerm) { // If the token is equal to // '+', '-' then // continue with the string if (pTerm == "+") { S += " + "; continue; } if (pTerm == "-") { S += " - "; continue; } // Otherwise find the differentiation // of that particular term else S += diffTerm(pTerm); } return S; } // Driver code int main() { string str = "5x^4 + 6x^2 + 5x^2"; cout << diffstr(str); return 0; } |
20X^3 + 12X^1 + 10X^1
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Program for Derivative of a Polynomial
- Program to find the indefinite Integration of the given Polynomial
- Sgn value of a polynomial
- Content of a Polynomial
- Integration in a Polynomial for a given value
- Finding nth term of any Polynomial Sequence
- Complete the sequence generated by a polynomial
- Horner's Method for Polynomial Evaluation
- Python | Finding Solutions of a Polynomial Equation
- Program to find value of 1^k + 2^k + 3^k + ... + n^k
- Program to find sum of 1 + x/2! + x^2/3! +...+x^n/(n+1)!
- Program to calculate value of nCr
- Program for n-th even number
- Program for n-th odd number
- Program to add two polynomials
- C program to calculate the value of nPr
- Program to add two fractions
- Program to compare m^n and n^m
- Program for sum of cos(x) series
- Program to Find the value of cos(nΘ)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

