/* the program accepts two polynomials as a input & prints the resultant polynomial because of the addition of input polynomials*/
#include
void main()
{
int poly1[6][2],poly2[6][2],term1,term2,match,proceed,i,j;
printf("Enter the number of terms in first polynomial. They must be less than 6:\n");
scanf("%d",&term1);
printf("Enter the number of terms in the second polynomial. They must be less than 6:\n");
scanf("%d",&term2);
printf("Enter the exponent and coefficient of every term of the first polynomial:\n");
for(i=0;i
{scanf("%d %d",&poly1[i][0],&poly1[i][1]);
}
printf("Enter the exponent and coefficient of every term of the second polynomial:\n");
for(i=0;i
{scanf("%d %d",&poly2[i][0],&poly2[i][1]);
}
printf("The resulting polynomial because of the addition of the input two polynomials:\n");
for(i=0;i
{
match=0;
for(j=0;j
{ if (match==0)
if(poly1[i][1]==poly2[j][1])
{ printf("%d %d\n",(poly1[i][0]+poly2[j][0]), poly1[i][1]);
match=1;
}
}
}
for(i=0;i
{
proceed=1;
for(j=0;j
{ if(proceed==1) if(poly1[i][1]!=poly2[j][1]) proceed=1;
else
proceed=0;
}
if (proceed==1)
printf("%d %d\n",poly1[i][0],poly1[i][1]);
}
for(i=0;i
{ proceed=1;
for(j=0;j
{ if(proceed==1) if(poly2[i][1]!=poly1[j][1]) proceed=1;
else
proceed=0;
}
if (proceed==1)
printf("%d %d",poly2[i][0],poly2[i][1]);
}
}
Output:
Enter the number of terms in first polynomial. They must be less than 6: 5. Enter the number of terms in the second polynomial .They must be less than 6: 4. Enter the coefficient & exponent of each of term of the first polynomial:
1 2
2 4
3 6
1 8
5 7
Enter the coefficient & exponent of every term of the second polynomial:
5 2
6 9
3 6
5 7
The resultant polynomial because of the addition of the input two polynomials:
6 2
6 6
10 7
2 4
1 8
6 9
The program prompted initially for number of terms of the two polynomials. Then, this prompted for the entry of terms of the 2 polynomials one after another. At first, this adds the coefficients of the corresponding terms of both the polynomials whose exponents are the similar. Then, this prints the terms of the primary polynomial who does not contain corresponding terms in the second polynomial along with the same exponent. Lastly, it prints the terms of the second polynomial that does not contain corresponding terms in the first polynomial.