# Addition of Two Polynomials Using Array in c++

Addition of Two Polynomials Using Array in c++. The logic of addition of two polynomial will remain same for all languages
#include<iostream.h>
#include<conio.h>
#include<string.h>
#define MAX 10
class polynomial
{
public:
struct term
{
int expo;
int coef;
} t[MAX];
int n;
polynomial()
{
n=0;
t[0].coef=0;
t[0].expo=0;
}
void setpoly(int no)
{
n=no;
int i;
cout<<“enter polynomial details \n”;
for(i=0;i<n;i++)
{
cout<<“\n enter coeff. : “;
cin>>t[i].coef;
cout<<“\n enter expo : “;
cin>>t[i].expo;

}
}
void display ()
{
cout<<“\n poly is \n”;
for(int i=0;i<n;i++)
{
cout<<” “<<t[i].coef<<“X^” <<t[i].expo<<” + “;
}
}
};
{
int terms,j,i,k=0;
polynomial p3;
int n1=p1.n;
int n2=p2.n;
terms=(n1>n2?n1:n2);
cout<<“max terms=”<<terms;
for(i=0,j=0;i<=terms;)
{
if(p1.t[i].expo>=p2.t[j].expo)
{
if(p1.t[i].expo>p2.t[j].expo)
{
p3.t[k].coef=p1.t[i].coef;
p3.t[k].expo=p1.t[i].expo;
cout<<” “<<p3.t[k].coef<<“X^” <<p3.t[k].expo<<” + “;
i++;
}
else
{
p3.t[k].coef=p1.t[i].coef+p2.t[j].coef;
p3.t[k].expo=p1.t[i].expo;
cout<<” “<<p3.t[k].coef<<“X^” <<p3.t[k].expo<<” + “;
i++;
j++;
}

}
else
{
p3.t[k].coef=p2.t[j].coef;
p3.t[k].expo=p2.t[j].expo;
cout<<” “<<p3.t[k].coef<<“X^” <<p3.t[k].expo<<” + “;
j++;
}
k++;
}
/* if(j<=n2)
{
cout<<“copying”;
while(j<=n2)
{
p3.t[k].coef=p2.t[j].coef;
p3.t[k].expo=p2.t[j].expo;
j++;
k++;
}
}
if(i<=n1)
{
cout<<“copying”;
while(i<=n1)
{
p3.t[k].coef=p1.t[i].coef;
p3.t[k].expo=p1.t[i].expo;
i++;
k++;
}
}*/
p3.n=k;
p3.display();

}
void main()
{
clrscr();
polynomial p1,p2;
int n;
cout<<“\n enter number of terms in poly “;
cin>>n;
p1.setpoly(n);
cout<<“\n enter number of terms in poly “;
cin>>n;
p2.setpoly(n);

p1.display();
p2.display();