The

**source code** for multiplication of the polynomial equation is given below with explanation. Inputs for this program are polynomial equations and their order.

**Calculating product of the polynomial: **This function takes two polynomial equations and their orders as a input stores the result in another array.

/****************************************************
Name: poly_mult
Input: equation one , order of equation one, equation two, order of eq two, result.
return: void
description: it will take the two polynomial equations and stores the result in prod_poly.
****************************************************/
void poly_mult(float equ1[], unsigned equ1_expo, float *equ2, unsigned equ2_expo, float *prod_poly)
{
unsigned i, j;
for(i=0;i<equ1_expo;i++)
{
for(j=0;j<equ2_expo;j++)
{
prod_poly[i+j]=(equ1[i] * equ2[j])+prod_poly[i+j];
// printf("i+j value is %f \n",prod_poly[i+j]);
}
}
}

**Displaying the polynomial Equation: **This function just take the input and displays as a polynomial equation form.

/****************************************************
Name: print_trxfunc
Input: equation, equation order
return: void
description: it just prints the given equation
****************************************************/
void print_trxfunc(float *equ, unsigned expo)
{
unsigned i;
printf("\t\t");
for(i=expo;i>0;i--)
{
if(equ[i]!=0 && equ[i]>0)
printf("+ %fx%u ", equ[i], i);
if(equ[i]!=0 && equ[i]<0)
printf(" %fx%u ", equ[i], i);
}
if(equ[0]!=0 && equ[0]>0)
printf("+ %f", equ[0]);
if(equ[0]!=0 && equ[0]<0)
printf(" %f", equ[0]);
printf("\n");
}

**Validating the characters: **This function checks for the valid characters in a equation. The valid characters are

**+, -, ., x, space, digits **other wise it returns

**zero **as invalid character.

/****************************************************
Name:check_char
Input: takes the char as a input
return: returns int
description: it will take the char as input and check for the
space, digit, 'x', and +,-, . symbols n returns the corresponding code otherwise zero it returns
****************************************************/
int check_char(char a)
{
if(isspace(a)) return 1;
else if(isdigit(a)) return 2;
else if(isalpha(a) && (a='x')) return 3;
else if(a=='+' || a=='-' ) return 4;
else if(a=='.') return 5;
return 0;
}

**Separating exponential: **This function will separate the exponential part from the given input equation and stores in a int varaibel. e.g 2x2+3x+4 in this equation , this functins separates 2 from 2x2 and 1 from 3x and zero from 4.

/****************************************************
Name: find_expo
Input: equation, expo, index
return: returns index
description: this function will separate the exponential of the each x and stores in expo.
****************************************************/
int find_expo(char *eq, int *expo,int *i)
{
char str_num[1024]={0};
int index = 0;
int dot_count = 0; //for float no.s only one dot should be there, more than means invalid equation
int code;
if(isalpha(eq[*i])) (*i)++; // this function works only if first char is alpha
else
{
printf("invalid equation \n");
exit(1);
}
// this for loop is copying numeric values to the arry str_num, which will be later converted to numeric using sscanf
for(;!isalpha(eq[*i]);(*i)++,index++)
{
code = check_char(eq[*i]);
if((code == 2)) // if digit , copy to array
str_num[index]=eq[*i];
else if (code == 4)
{
//printf("breaking the loop\n");
break;
}
}
str_num[index]='\0'; // appending the termination char to the array
int temp;
if(!index)
{
printf("invalid equation\n");
exit(1);
}
sscanf(str_num,"%d",&temp);
*expo = temp;
return index;
}

**Storing in array: **This function will convert the equation and stores in memory in the form of array such that exponential is index of the array and coefficient is the value of that index. e.g for 2x2+3x+4 this equation, arry[2] is 2 arry[1] is 3 and arry[0] is zero

/****************************************************
Name: store_coeff_arry
Input: equation, arry
return: void
description: this function will separate the coeff n expo from input equation and
stores them in an array. Internally it will call different functuinc to find coeff n expo
****************************************************/
void store_coeff_arry(char *eq, float *arry)
{
int eq_len = strlen(eq);
int i=0;
int sign = 0;
int init_sign = 1;
int sts;
int pos=0;
float coeff;
int expo=0;
for(;i<eq_len;i++)
{
if(isspace(eq[i]))
continue;
sts=check_char(eq[i]);
if( (sts == 4)) //is a sign
{
sign=(eq[i]=='+') ? 1 : -1;
pos = find_coeff(eq,&coeff,&i);
i--;
pos=0;
}
else if(sts == 3 ) // is a alphabet
{
pos=find_expo(eq,&expo,&i);
i--;
pos=0;
arry[expo] = coeff*sign;
sign=0;
coeff=0;
expo=0;
}
}
}

**Separating the coefficients: **This function separates the coefficients from the given equation and stores in a variable. e.g 2x2+3x+4 , this function separates 2, 3 and 4 as a coefficients

/****************************************************
Name: find_coeff
Input: equation , coeff, index
return: index
description: this function separates the coef from given input and stores in coeff and retunrs the index.
****************************************************/
int find_coeff(char *eq, float *coeff,int *i)
{
char str_num[1024]={0};
int index = 0;
int dot_count = 0; //for float no.s only one dot shouls be there, more than means invalid equation
int code;
if((eq[*i]=='+')|| (eq[*i]=='-'))
(*i)++; // this function works only if first char is sign
else
{
printf("invalid euation \n");
exit(1);
}
// this for loop is copying numeric values to the arry str_num, which will be later converted to numeric using sscanf
for(;!isalpha(eq[*i]);(*i)++,index++)
{
code = check_char(eq[*i]);
if((code == 2)) // if digit , copy to array
str_num[index]=eq[*i];
else if((code == 5)&& (!dot_count)) //if dot add to array n increment the dot count
{
str_num[index]=eq[*i];
dot_count++;
}
else if(code == 4)
{
puts("In-correct equation format entered");
exit(1);
}
}
str_num[index]='\0'; // appending the termination char to the array
float temp;
if(!index)
{
printf("invalid equation\n");
exit(1);
}
sscanf(str_num,"%f",&temp);
*coeff = temp;
return index;
}

**Main function: **This function asks for the order and equations , then calls

*store_coeff_arry() *and stores the coeff and expo in an array and then it calls the

*prod_mult() *to calculate the product and calls

*print_trxfunc() *to display the result in a equation format.

main()
{
char eq[1024];
float *prod;
float *poly1, *poly2;
unsigned poly1_ord, poly2_ord;
printf(" \nPoints to remember:\n");
printf("1. Every coefficient should be represented numerically, i.e. x should be written as 1x1\n");
printf("2. Every exponent should be represented numerically, i.e. 1 should be written as 1x0\n");
printf("3. Example equation is: -10x7+23x9+ 137x3 -1x1 +3x0\n");
printf("4. No space should be provided between coefficient and variable & coefficient and exponent.\n\n");
printf("What will be the order of the polynomial one:\n");
scanf("%u", &poly1_ord);
printf("\n Enter polynomial equation : e.g +2x1+3x0 for 2x+3 \n ");
scanf("%s", &eq);
if(eq[0]!='+' && eq[0]!='-')
{
printf("invalid equation, please enter with sign \n");
exit(0);
}
if((poly1=(float*)calloc(poly1_ord, sizeof(float)))==NULL)
{
printf("\n\nNot enough memory space\n\n");
exit(1);
}
//storing the coeff in poly1
store_coeff_arry(eq,poly1);
printf("What will be the order of polynomial two:");
scanf("%u",&poly2_ord);
printf("\n Enter polynomial equation : e.g +2x1+3x0 for 2x+3 \n ");
scanf("%s",&eq);
if(eq[0]!='+' && eq[0]!='-')
{
printf("invalid equation, please enter with sign \n");
exit(0);
}
if((poly2=(float*)calloc(poly2_ord, sizeof(float)))==NULL)
{
printf("\n\nNot enough memory space\n\n");
exit(1);
}
//storing the coeff in poly2
store_coeff_arry(eq, poly2);
if((prod=(float*)calloc(poly1_ord+poly2_ord, sizeof(float)))==NULL)
{
printf("\n\nNot enough memory space\n\n"); exit(1);
}
//calculating the product n storing in prod
poly_mult(poly1, poly1_ord, poly2, poly2_ord, prod);
// printing the result
print_trxfunc(prod, poly1_ord+poly2_ord);
free(prod);
free(poly2);
free(poly1);
}

**Input format: **
**2x+3** should be given as

**+2x1+3x0**
**-3x2+4x+5** should be given as

**-3x2+4x1+5x0**
Other wise results are undefined.

**Sample Output:** (2x+3)(2x+3)

+ 4.000000x2 + 12.000000x1 + 9.000000

P.S: Actually one of my cousin asked me the code for product of a polynomial equation. I used google for getting the code, but I could not get. So I thought of sharing this code. Just copy this code and try to compile in *nix system. you may get some compile time errors in windows(not sure).