0 like 0 dislike
792 views
Represent a 2-variable polynomial using array. Use this representation to implement addition of polynomials.

0 like 0 dislike
by Goeduhub's Expert (7.6k points)
edited

## Ques . Write a program to show addition of two polynomial

Example :

A[] = {2, 1, 1, 6}
B[] = {1, 2, 4}
sum[] = {3, 3, 17, 6}

A[] : 2 + 1x^1 + 1x^2 + 6x^3
B[] : 1 + 2x^1 + 4x^2
Sum[] : 3 + 3x^1 + 17x^2 + 6x^3

Program :

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 != 0):

print("x^", i, end = "")

if (i != n - 1):

print(" + ", end = "")

A = [5, 0, 10, 6]

B = [1, 2, 4]

m = len(A)

n = len(B)

print("enter First polynomial is")

printPoly(A, m)

print("", end = "")

print("\n enter Second polynomial is")

printPoly(B, n)

print("", end = "")

sum = add(A, B, m, n)

size = max(m, n)

print("\nsum polynomial is")

printPoly(sum, size)

Output :

enter First polynomial is

5 + 0x^ 1 + 10x^ 2 + 6x^ 3

enter Second polynomial is

1 + 2x^ 1 + 4x^ 2

sum polynomial is

6 + 2x^ 1 + 14x^ 2 + 6x^ 3

## For more Rajasthan Technical University CSE-III Sem DSA Lab Experiments  CLICK HERE

Learn & Improve In-Demand Data Skills Online in this Summer With  These High Quality Courses[Recommended by GOEDUHUB]:-

Best Data Science Online Courses[Lists] on:-

Claim your 10 Days FREE Trial for Pluralsight.