adding of two polynomials programs in python
def add(A, B, m, n):
size = max(m, n);
sum = [0 for i in range(size)]
for i in range(0, m, 1):
sum[i] = A[i]
for i in range(n):
sum[i] += B[i]
return sum
def printPoly(poly, n):
for i in range(n):
print(poly[i], end = "")
if i == 1:
print('x',end="")
elif (i != 0):
print("x^", i, end = "")
if (i != n - 1):
print(" + ", end = "")
#Polynomial 5 + 7x + 10x^2 + 6x^3
A = [5, 7, 10, 6]
# polynomial 1 + 2x + 4x^2
B = [1, 2, 4]
m = len(A)
n = len(B)
sum = add(A, B, m, n)
size = max(m, n)
#Print sum of polynomial
print("\n\nsum polynomial is")
printPoly(sum, size)
print()
Comments
Leave a comment