Answer to Question #255952 in Python for gandhi

Question #255952

adding of two polynomials programs in python


1
Expert's answer
2021-10-24T18:49:07-0400
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()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS