You are on page 1of 7

ENGINEERING

MATHEMATICS - 1
(BAS-103)

FINDING INDEFINITE
INTEGRAL OF A GIVEN
POLYNOMIAL USING
PYTHON
# Python3 program to find the indefinite
# integral of the given polynomial
MOD = 1000000007

# Function to perform the integral


# of each term
def inteTerm( pTerm):

# Get the coefficient


coeffStr = ""
S = "";

# Loop to iterate and get the


# Coefficient
i=0
while pTerm[i] != 'x':
coeffStr += (pTerm[i]);
i += 1
coeff = int(coeffStr)

powStr = "";

# Loop to find the power


# of the term
for j in range(i + 2, len(pTerm)):
powStr += (pTerm[j]);

power = int(powStr)
a = ""
b = "";

# For ax^n, we find a*x^(n+1)/(n+1)


str1 = coeff;
a = str1
power += 1
str2 = power;
b = str2
S += "(" + str(a) + "/" + str(b) + ")X^" + str(b);

return S;

# Function to find the indefinite


# integral of the given polynomial
def integrationVal(poly):

# We use istringstream to get the


# input in tokens
is1 = poly.split();

S = "";

# Loop to iterate through


# every term
for pTerm in is1:

# 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
str1 = "5x^3 + 7x^1 + 2x^2 + 1x^0";
print(integrationVal(str1) + " + C ");

# This code is contributed by phasing17

You might also like