Given two polynomials A and B, write a program that adds the given two polynomials A and B.
Input
5
0 -2
3 6
4 7
1 -3
2 -1
5
0 1
1 2
2 -4
3 3
4 5
Your Output
12x^4 + 9x^3 - 5x^2 - 1x - 1
Expected
12x^4 + 9x^3 - 5x^2 - x - 1
Please Find Error
import math
import numpy
def DispPoly(C,n):
C = C[::-1]
s = ""
k=n-1
for r in range(0,len(C)-2):
if(r==0):
if(C[r]>0): s = s + str(C[r])+"x^"+str(k)
if(C[r]<0): s = s + str(C[r])+"x^"+str(k)
if(r>0):
if(C[r]>0): s = s + "+" + str(C[r])+"x^"+str(k)
if(C[r]<0): s = s + str(C[r])+"x^"+str(k)
k=k-1
if(C[n-2]>0): s = s + "+" + str(C[n-2]) + "x"
if(C[n-2]<0): s = s + str(C[n-2]) + "x"
if(C[n-1]>0): s = s + "+" + str(C[n-1])
if(C[n-1]<0): s = s + str(C[n-1])
print(s)
N = int(input("Enter the Order of Polynomial: "))
N = N+1
print("Enter coefficients of the two polynomiuals separated by SPACE: ")
Coeff=[]
AddP=[]
for r in range(0,N):
s = "Enter coeff. of x^" + str(r) + str(": ")
t = (str(input(s))).split(" ")
temp = []
temp.append(int(t[0]))
temp.append(int(t[1]))
Coeff.append(temp)
AddP.append(sum(temp))
Coeff = np.array(Coeff)
Coeff=np.transpose(Coeff)
print("\nPolynomial-1:")
DispPoly(Coeff[0],N)
print("\nPolynomial-2:")
DispPoly(Coeff[1],N)
print("\nAfter Adding two polynomials: ")
DispPoly(AddP,N)
Comments
Leave a comment