Integration in a Polynomial for a given value
Last Updated :
12 Jul, 2025
Given string str which represents a polynomial and a number N, the task is to integrate this polynomial with respect to X at the given value N.
Examples:
Input: str = "90x4 + 24x3 + 18x2 + 18x", N = 1.
Output: 39
Explanation:
Given, dy/dx = 90*(X4) + 24* (X3) + 18* (X2) + 18*(X). On integrating this equation, we get 18*(X5) + 6*(X4) + 6*(X3) + 9*(X2) and substituting the value X = 1, we get:
18 + 6 + 6 + 9 = 39.
Input: str = "4x3 + 2x2 + 3x", N = 2
Output: 27
Approach: The idea is to use the identity of the integration. For some given function X with the power of N, the integration of this term is given by:
\int (X^{N}) = \frac{X^{N + 1}}{N + 1}
Therefore, the following steps are followed to compute the answer:
- Get the string.
- Split the string and perform the integration based on the above formula.
- Substitute the value of N in the obtained expression.
- Add all the individual values to get the final integral value.
Below is the implementation of the above approach:
C++
// C++ program to find the integration
// of the given polynomial for the
// value N
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// Function to return the integral
// of the given term
double inteTerm(string pTerm, ll val)
{
// Get the coefficient
string coeffStr = "";
int i;
// Loop to iterate through the string
// and get the coefficient
for (i = 0; pTerm[i] != 'x'; i++)
coeffStr.push_back(pTerm[i]);
ll coeff = atol(coeffStr.c_str());
// Get the Power
string powStr = "";
// Loop to skip 2 characters for x and ^
for (i = i + 2; i != pTerm.size(); i++)
powStr.push_back(pTerm[i]);
ll power = atol(powStr.c_str());
// Return the computed integral
return (coeff * pow(val, power + 1))
/ (power + 1);
}
// Functionto find the integration
// of the given polynomial for the
// value N
double integrationVal(string poly, int val)
{
ll ans = 0;
// Using string stream to get the
// input in tokens
istringstream is(poly);
string pTerm;
while (is >> pTerm) {
// If the token is equal to '+' then
// continue with the string
if (pTerm == "+")
continue;
// Otherwise find the integration
// of that particular term
else
ans = (ans + inteTerm(pTerm, val));
}
return ans;
}
// Driver code
int main()
{
string str = "4x^3 + 3x^1 + 2x^2";
int val = 2;
cout << integrationVal(str, val);
return 0;
}
Java
// Java program for the above approach
public class GFG
{
// Function to return the integral
// of the given term
static int inteTerm(String pTerm, int val)
{
// Get the coefficient
String coeffStr = "";
// Loop to iterate through
// the string and get the
// coefficient
int i = 0;
while (i < pTerm.length() && pTerm.charAt(i) != 'x')
{
coeffStr += pTerm.charAt(i);
i += 1;
}
int coeff = Integer.parseInt(coeffStr);
// Get the Power
String powStr = "";
// Loop to skip 2 characters
// for x and ^
int j = i + 2;
while(j< pTerm.length())
{
powStr += (pTerm.charAt(j));
j += 1;
}
int power = Integer.parseInt(powStr);
// Return the computed integral
return ((coeff * (int)Math.pow(val, power + 1)) / (power + 1));
}
// Functionto find the integration
// of the given polynomial for the
// value N
static int integrationVal(String poly, int val)
{
int ans = 0;
// Using string stream to
// get the input in tokens
String[] stSplit = poly.split(" \\+ ");
int i = 0;
while(i < stSplit.length)
{
ans = (ans + inteTerm(stSplit[i], val));
i += 1;
}
return ans;
}
// Driver code
public static void main(String[] args) {
String st = "4x^3 + 3x^1 + 2x^2";
int val = 2;
System.out.println(integrationVal(st, val));
}
}
// This code is contributed by divyesh072019.
Python3
# Python3 program to find
# the integration of the
# given polynomial for the
# value N
# Function to return the integral
# of the given term
def inteTerm(pTerm, val):
# Get the coefficient
coeffStr = ""
# Loop to iterate through
# the string and get the
# coefficient
i = 0
while (i < len(pTerm) and
pTerm[i] != 'x'):
coeffStr += pTerm[i]
i += 1
coeff = int(coeffStr)
# Get the Power
powStr = ""
# Loop to skip 2 characters
# for x and ^
j = i + 2
while j< len(pTerm):
powStr += (pTerm[j])
j += 1
power = int(powStr)
# Return the computed integral
return ((coeff *
pow(val,
power + 1)) //
(power + 1))
# Functionto find the integration
# of the given polynomial for the
# value N
def integrationVal(poly, val):
ans = 0
# Using string stream to
# get the input in tokens
stSplit = poly.split("+")
i = 0
while i < len(stSplit):
ans = (ans +
inteTerm(stSplit[i],
val))
i += 1
return ans
# Driver code
if __name__ == "__main__":
st = "4x^3 + 3x^1 + 2x^2"
val = 2
print(integrationVal(st, val))
# This code is contributed by Chitranayal
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG {
// Function to return the integral
// of the given term
static int inteTerm(string pTerm, int val)
{
// Get the coefficient
string coeffStr = "";
// Loop to iterate through
// the string and get the
// coefficient
int i = 0;
while (i < pTerm.Length && pTerm[i] != 'x')
{
coeffStr += pTerm[i];
i += 1;
}
int coeff = Convert.ToInt32(coeffStr);
// Get the Power
string powStr = "";
// Loop to skip 2 characters
// for x and ^
int j = i + 2;
while(j< pTerm.Length)
{
powStr += (pTerm[j]);
j += 1;
}
int power = Convert.ToInt32(powStr);
// Return the computed integral
return ((coeff * (int)Math.Pow(val, power + 1)) / (power + 1));
}
// Functionto find the integration
// of the given polynomial for the
// value N
static int integrationVal(string poly, int val)
{
int ans = 0;
// Using string stream to
// get the input in tokens
string[] stSplit = poly.Split('+');
int i = 0;
while(i < stSplit.Length)
{
ans = (ans + inteTerm(stSplit[i], val));
i += 1;
}
return ans;
}
// Driver code
static void Main() {
string st = "4x^3 + 3x^1 + 2x^2";
int val = 2;
Console.WriteLine(integrationVal(st, val));
}
}
// This code is contributed by divyeshrabadiya07.
JavaScript
<script>
// Javascript program for the above approach
// Function to return the integral
// of the given term
function inteTerm(pTerm,val)
{
// Get the coefficient
let coeffStr = "";
// Loop to iterate through
// the string and get the
// coefficient
let i = 0;
while (i < pTerm.length && pTerm[i] != 'x')
{
coeffStr += pTerm[i];
i += 1;
}
let coeff = parseInt(coeffStr);
// Get the Power
let powStr = "";
// Loop to skip 2 characters
// for x and ^
let j = i + 2;
while(j< pTerm.length)
{
powStr += (pTerm[j]);
j += 1;
}
let power = parseInt(powStr);
// Return the computed integral
return Math.floor((coeff * Math.floor(Math.pow(val, power + 1))) / (power + 1));
}
// Functionto find the integration
// of the given polynomial for the
// value N
function integrationVal(poly,val)
{
let ans = 0;
// Using string stream to
// get the input in tokens
let stSplit = poly.split(" + ");
let i = 0;
while(i < stSplit.length)
{
ans = (ans + inteTerm(stSplit[i], val));
i += 1;
}
return ans;
}
// Driver code
let st = "4x^3 + 3x^1 + 2x^2";
let val = 2;
document.write(integrationVal(st, val));
// This code is contributed by avanitrachhadiya2155
</script>
Similar Reads
Program to find the indefinite Integration of the given Polynomial 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 = "4X3 + 3X1 + 2X2" Output: X4 + (3/2)X2 + (2/3)X3 + C Input: str = "5
8 min read
Sgn value of a polynomial Given a polynomial function f(x) = 1+ a1*x + a2*(x^2) + ... an(x^n). Find the Sgn value of these function, when x is given and all the coefficients also. If value of polynomial greater than 0 Sign = 1 Else If value of polynomial less than 0 Sign = -1 Else if value of polynomial is 0 Sign = 0 Example
6 min read
Polynomials in One Variable | Polynomials Class 9 Maths Polynomials in One Variable: Polynomial word originated from two words âpolyâ which means âmanyâ and the word ânominalâ which means âtermâ. In maths, a polynomial expression consists of variables known as indeterminate and coefficients. Polynomials are expressions with one or more terms with a non-z
7 min read
Program for finding the Integral of a given function using Boole's Rule Given a function, f(x), tabulated at points x_i equally spaced by h=x_{i+1} - x_i such that f_1 = f(x_1) f_2=f(x_2) .....and so on The upper and lower limits a, b correspond to which the integral needs to be found, the task is to find the integral value of the given equation f(x).Examples: Input: a
8 min read
Program to find the value of P(N + r) for a polynomial of a degree N such that P(i) = 1 for 1 ≤ i ≤ N and P(N + 1) = a Given three positive integers N, R, and A and a polynomial P(X) of degree N, P(i) = 1 for 1 ? i ? N and the value of P(N + 1) is A, the task is to find the value of the P(N + R). Examples: Input: N = 1, R = 3, A = 2Output: 4Explanation:The equation of the given polynomial is P(x) = x. Therefore, the
7 min read
Program for Simpson's 1/3 Rule In numerical analysis, Simpson's 1/3 rule is a method for numerical approximation of definite integrals. Specifically, it is the following approximation:Â $$\int_{a}^{b} f(x) dx \approx \frac{(b-a)}{6} \bigg(f(a) + 4f \frac{(a+b)}{2} + f(b)\bigg)$$Â Â Â Â Â Â In Simpson's 1/3 Rule, we use parabolas to appr
7 min read