# Multiplication of polynomials in C++

In this tutorial, we will learn how to perform multiplication of polynomials in C++.

Before proceeding to the solution, let us understand what a polynomial is:

• A polynomial is an expression.
• It consists of variables that are also called indeterminates and coefficients.
• The operations are addition, subtraction, multiplication, and non-negative integer exponents of variables.

Example of a polynomial:

`5x^3 + 4x^2 + 7x^1 + 12`

This polynomial has four terms. The first term has coefficient 5 and the degree of the variable x is 3. The next two terms have coefficients 4 and 7 with degrees 2 and 1 respectively. The last term has a coefficient of 12 and the degree is 0. So, we can omit writing the variable as any variable to the power of zero equates to 1.

## Program to perform multiplication of polynomials in C++:

```#include<iostream>
using namespace std;
int multiplypolynomial(int (&x),int (&y),int (&z),int num1,int num2)
{
int r=0;
for(int m=0;m<num2;m++)
{
for(int n=0;n<num1;n++,r++)
{
z[r]=x[n]*y[m];
z[r]=x[n]+y[m];
}
}
for(int m=0;m<r;m++)
{
for(int n=m+1;n<r;n++)
{
if(z[m]==z[n])
{
z[m]+=z[n];
for(int l=n;l<r-1;l++)
{
z[l]=z[l+1];
z[l]=z[l+1];
}
r--;
}
}
}
return r;
}
void printpolynomial(int (&g),int num)
{
for(int j=0;j<num-1;j++)
cout<<g[j]<<"*x^"<<g[j]<<"+";
cout<<g[num-1]<<"*x^"<<g[num-1]<<endl;
}
void getpolynomial(int (&g),int &num)
{
cout<<"How many terms are there in the polynomial ?"<<endl;
cin>>num;
cout<<"Enter the coefficient and the exponent of the polynomial terms in the decreasing order: "<<endl;
for(int j=0;j<num;j++)
cin>>g[j]>>g[j];
}
int main()
{
int x,y,z,num1,num2,num3;
getpolynomial(x,num1);
printpolynomial(x,num1);
getpolynomial(y,num2);
printpolynomial(y,num2);
num3=multiplypolynomial(x,y,z,num1,num2);
cout<<"The result of the multiplication of two polynomials: "<<endl;
for(int j=0;j<num3-1;j++)
cout<<z[j]<<"*x^"<<z[j]<<"+";
cout<<z[num3-1]<<"*x^"<<z[num3-1]<<endl;
return 0;
}```

Output:

```How many terms are there in the polynomial ?
3
Enter the coefficient and the exponent of the polynomial terms in the decreasing order:
5
3
4
2
7
1
5*x^3+4*x^2+7*x^1
How many terms are there in the polynomial ?
2
Enter the coefficient and the exponent of the polynomial terms in the decreasing order:
5
1
4
0
5*x^1+4*x^0
The result of the multiplication of two polynomials:
25*x^4+40*x^3+51*x^2+28*x^1```

The above program performs the multiplication of two polynomials. It accepts the count of the number of terms. It then accepts the coefficients and the degrees of the polynomial terms. Then it performs multiplication by considering each variable in both the polynomials.

For this use the two-dimensional arrays to store the polynomial terms along with its corresponding degree. Then after the multiplication operation, again store the result in a two-dimensional array. Finally, display the result on the terminal.