Last Updated:

# Polynomial Addition using Structure

## Algorithm

Input: Enter the polynomials to add.
Output: The added polynomial.
1. Start
2. ch[] = {'+','\0'},count = 0
3. Read the number of elements of first polynomial and store it in n1.
4. Read the number of elements of second polynomial and store it in n2.
5. s = n1 + n2
6. Read the terms of first polynomial and store it in the structure poly poly1[n1].
7. for i=0 to i<n1
8. Read the terms of second polynomial and store it in the structure poly poly2[n2].
9. for j=0 to j<n2
10. i = 0,j = 0,k = 0
11. while(i<n1 and j<n2)
11.1. if poly1[i].exp==poly2[j].exp
11.1.1. sum[k].coeff=poly1[i].coeff+poly2[j].coeff
11.1.2. sum[k].exp=poly1[i].exp
11.1.3. i++,j++,k++,count++
11.2. else
11.2.1. if poly1[i].exp>poly2[j].exp
11.2.1.1. sum[k].exp=poly1[i].exp
11.2.1.2. sum[k].coeff=poly1[i].coeff
11.2.1.3. i++,k++,count++
11.2.2. else
11.2.2.1. sum[k].exp=poly2[j].exp
11.2.2.2. sum[k].coeff=poly2[j].coeff
11.2.2.3. j++,k++,count++
12. if i>n1
12.1. while(j<n2)
12.1.1. sum[k].exp=poly2[j].exp
12.1.2. sum[k].coeff=poly2[j].coeff
12.1.3. j++,k++,count++
13. else
13.1. while(i<n1)
13.1.1. sum[k].exp=poly1[i].exp
13.1.2. sum[k].coeff=poly1[i].coeff
13.1.3. i++,k++,count++
14. Print "The first polynomial is:"
15. for i=0 to i<n1
15.1. if poly1[i].coeff>=0 && i!=0
15.1.1. str=ch[0]
15.2. else
15.2.1. str=ch[1]
15.3. if i!=n1
15.3.1. Print str
15.4. Print poly1[i].coeff,poly1[i].exp
16. Print "The second polynomial is:"
17. for j=0 to j<n2
17.1. if poly2[j].coeff>=0 && j!=0
17.1.1. str=ch[0]
17.2. else
17.2.1. str=ch[1]
17.3. if (j!=n2)
17.3.1. Print str
17.4. Print poly2[j].coeff,poly2[j].exp
18. Print "The polynomial sum is:"
19. for k=0 to k<count
19.1. if(sum[k].coeff>=0 and k!=0)
19.1.1. str=ch[0]
19.2. else
19.2.1. str=ch[1]
19.3. if k!=count
19.3.1. Print str
19.4. Print sum[k].coeff,sum[k].exp
20. Stop.

## C Program

#include<stdio.h>
struct poly {
int coeff;
int exp;
};
void main() {
int n1,n2,i=0,j=0,k=0,count=0;
char ch[]={'+','\0'};
char str;
printf("Enter the number of terms of first polynomial:");
scanf("%d",&n1);
printf("Enter the numbr of terms of second polynomial:");
scanf("%d",&n2);
struct poly poly1[n1];
struct poly poly2[n2];
int s=n1+n2;
struct poly sum[s];
printf("PLEASE ENTER THE TERMS IN THE DECREASING POWER\n");
printf("Polynomial 1\n");
for(i;i<n1;i++) {
printf("Enter the coefficient of %d term: ",i+1);
scanf("%d",&poly1[i].coeff);
printf("Enter the exponent of %d term: ",i+1);
scanf("%d",&poly1[i].exp);
}
printf("Polynomial 2\n");
for(j;j<n2;j++) {
printf("Enter the coefficient of %d term: ",j+1);
scanf("%d",&poly2[j].coeff);
printf("Enter the exponent of %d term: ",j+1);
scanf("%d",&poly2[j].exp);
}
i=0,j=0,k=0;
while(i<n1 && j<n2) {
if(poly1[i].exp==poly2[j].exp) {
sum[k].coeff=poly1[i].coeff+poly2[j].coeff;
sum[k].exp=poly1[i].exp;
i++,j++,k++,count++;
} else {
if(poly1[i].exp>poly2[j].exp) {
sum[k].exp=poly1[i].exp;
sum[k].coeff=poly1[i].coeff;
i++,k++,count++;
} else {
sum[k].exp=poly2[j].exp;
sum[k].coeff=poly2[j].coeff;
j++,k++,count++;
}
}
}
if(i>n1) {
while(j<n2) {
sum[k].exp=poly2[j].exp;
sum[k].coeff=poly2[j].coeff;
j++,k++,count++;
}
} else {
while(i<n1) {
sum[k].exp=poly1[i].exp;
sum[k].coeff=poly1[i].coeff;
i++,k++,count++;
}
}
i=0,j=0,k=0;
printf("The 1st polynomial is: ");
for(i;i<n1;i++) {
if(poly1[i].coeff>=0 && i!=0) {
str=ch[0];
} else {
str=ch[1];
}
if(i!=n1) {
printf("%c ",str);
}
printf("%dx^%d ",poly1[i].coeff,poly1[i].exp);
}
printf("\nThe 2nd polynomial is: ");
for(j;j<n2;j++) {
if(poly2[j].coeff>=0 && j!=0) {
str=ch[0];
} else {
str=ch[1];
}
if(j!=n2) {
printf("%c ",str);
}
printf("%dx^%d ",poly2[j].coeff,poly2[j].exp);
}
printf("\nThe polynomial sum is: ");
for(k;k<count;k++) {
if(sum[k].coeff>=0 && k!=0) {
str=ch[0];
} else {
str=ch[1];
}
if(k!=count) {
printf("%c ",str);
}
printf("%dx^%d ",sum[k].coeff,sum[k].exp);
}
}

## Output

Enter the number of terms of first polynomial:3
Enter the numbr of terms of second polynomial:2
PLEASE ENTER THE TERMS IN THE DECREASING POWER
Polynomial 1
Enter the coefficient of 1 term: 4
Enter the exponent of 1 term: 2
Enter the coefficient of 2 term: 3
Enter the exponent of 2 term: 1
Enter the coefficient of 3 term: 7
Enter the exponent of 3 term: 0
Polynomial 2
Enter the coefficient of 1 term: 2
Enter the exponent of 1 term: 1
Enter the coefficient of 2 term: 5
Enter the exponent of 2 term: 0
The 1st polynomial is:   4x^2 + 3x^1 + 7x^0
The 2nd polynomial is:   2x^1 + 5x^0
The polynomial sum is:   4x^2 + 5x^1 + 12x^0