#include<stdio.h>
#include<conio.h>
void main()
{
int a[5][2],b[5][2],c[7][2],s=0,h=0,k=0,v,n,i,j,d;
clrscr();
printf("\nenter the degree of first polynomial");
scanf("%d",&n);
printf("\nenter the degree of the second polynomial");
scanf("%d",&v);
printf("\nenter the exponents of the polynomials in decreasing order\n");
for(i=0;i<=n;i++)
{
printf("\nenter the %d exponent and coefficient of the first polynomial",i+1);
scanf("%d %d",&a[i][0],&a[i][1]);
}
for(j=0;j<=v;j++)
{
printf("\nenter the %d exponent and coefficient of the second polynomial",j+1);
scanf("%d %d",&b[j][0],&b[j][1]);
}
while(s<=n && h<=v)
{
if(a[s][0]==b[h][0])
{
c[k][0]=a[s][0];
c[k][1]=a[s][1]+b[h][1];
s++;
h++;
k++;
}
else if(a[s][0]<b[h][0])
{
c[k][0]=b[h][0];
c[k][1]=b[h][1];
k++;
h++;
}
else
{
c[k][0]=a[s][0];
c[k][1]=a[s][1];
k++;
s++;
}
}
while(s<=n)
{
c[k][0]=a[s][0];
c[k][1]=a[s][1];
k++;
s++;
}
while(h<=v)
{
c[k][0]=b[h][0];
c[k][1]=b[h][1];
k++;
h++;
}
printf("\nthe resultant polynomial is given by");
for(d=0;d<k;d++)
printf("\nthe exponent is %d and coeff. is %d",c[d][0],c[d][1]);
getch();
}