
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program for Derivative of a Polynomial
Given a string containing the polynomial term, the task is to evaluate the derivative of that polynomial.
What is a Polynomial?
Polynomial comes from two words: - “Poly” which means “many” and “nomial” means “terms”, which comprises many terms. Polynomial expression is an expression containing variables, coefficients and exponents, which only involves operations such as, addition, multiplication and subtraction of variable(s).
Example of polynomial
x2+x+1
Derivative of the polynomial p(x) = mx^n will be −
m * n * x^(n-1)
Example
Input: str = "2x^3 +1x^1 + 3x^2" val = 2 Output: 37 Explanation: 6x^2 + 1x^0 + 6x^1 Putting x = 2 6*4 + 1 + 6*2 = 24 + 1 + 12 = 37 Input: str = “1x^3” val = 2 Output: 12 Explanation: 1 * 3 *x^2 Putting x = 2 3 * 4 = 12
Approach we will be using to solve the above problem −
- Take the input as a string and a value of x
- Now traverse the string and check for the digits, and variables.
- Keep adding and traversing the string till we find ‘+’.
- Then m * n * x^(n-1).
- Return the result.
Algorithm
Start Step 1-> In function long long term(string polyterm, long long val) Declare and initialize coeffStr = "” Declare i Loop For i = 0 and polyterm[i] != 'x' and i++ Call coeffStr.push_back(polyterm[i]) Set coeff = atol(coeffStr.c_str() Declare and initialize powStr = "" Loop For i = i + 2 and i != polyterm.size() and i++ powStr.push_back(polyterm[i]) Set power = atol(powStr.c_str()); Return coeff * power * pow(val, power - 1) Step 2-> In function long long value(string& str, int val) Set ans = 0 Call istringstream is(str) Declare string polyterm Loop While is >> polyterm If polyterm == "+” then, Continue Else Set ans = (ans + term(polyterm, val)) Return ans Step 3-> In function int main() Declare and initialize str = "2x^3 + 1x^1 + 3x^2" Declare and initialize val = 2 Print the value received by value(str, val) Stop
Example
#include using namespace std; long long term(string polyterm, long long val) { //to find the coefficient string coeffStr = ""; int i; for (i = 0; polyterm[i] != 'x'; i++) coeffStr.push_back(polyterm[i]); long long coeff = atol(coeffStr.c_str()); // to get the power value string powStr = ""; for (i = i + 2; i != polyterm.size(); i++) powStr.push_back(polyterm[i]); long long power = atol(powStr.c_str()); // For ax^n, we return a(n-1)x^(n-1) return coeff * power * pow(val, power - 1); } long long value(string& str, int val) { long long ans = 0; // using istringstream to get input in tokens istringstream is(str); string polyterm; while (is >> polyterm) { // check if the token is equal to '+' then // continue with the string if (polyterm == "+") continue; // Otherwise find the derivative of that // particular term else ans = (ans + term(polyterm, val)); } return ans; } // main function int main() { string str = "2x^3 + 1x^1 + 3x^2"; int val = 2; cout << value(str, val); return 0; }
Output
37
- Related Articles
- Frequency Derivative Property of Fourier Transform
- Find the derivative of $V=2x^3+3x+9$.
- Python program to compute a Polynomial Equation
- Python Program to Compute a Polynomial Equation given that the Coefficients of the Polynomial are stored in a List
- C program to compute the polynomial regression algorithm
- Program for factorial of a number in C program
- Python Program for factorial of a number
- C Program for Circumference of a Parallelogram
- What Is A Polynomial ?
- Compute the roots of a polynomial in Python
- Evaluate a polynomial and every column of coefficients in r is evaluated for every element of x in Python
- C Program for Program to find the area of a circle?
- C Program for focal length of a lens
- Integrate a polynomial in Python
- Differentiate a polynomial in Python

Advertisements