## Polynomial addition using arrays in C

Program Description

Polynomial addition in C using arrays
This program shows the implementation of polynomial addition using arrays.
The program expects the user to enter the polynomial with integer exponent term while it supports floating value for coefficient terms.The program takes the values for the polynomial terms in the descending order,starting from the highest degree terms in the beginning and lowest degree terms in the end.

The coefficients and exponent of the polynomial are stored in a structure. An array of such structure is used to represent a polynomial.
The program takes two polynomials as input and gives out the sum of the two polynomials.

Note: Since we are using the header file,don't forget to link the math library while compiling this code with gcc.
gcc filename.c -lm

Program
```/*********************************************
*    Polynomial addition using arrays in C
*
*	code.cheraus.com
*
*********************************************/

#include<stdio.h>
#include<math.h>

/*
This structure is used to store a polynomial term. An array of such terms represents a
polynomial.
The "coeff" element stores the coefficient of a term in the polynomial,while
the "exp"   element stores the exponent.

*/
struct poly
{
float coeff;
int exp;
};

//declaration of polynomials
struct poly a,b,c,d;

int main()
{
int i;
int deg1,deg2;      //stores degrees of the polynomial
int k=0,l=0,m=0;

printf("Enter the highest degree of poly1:");
scanf("%d",&deg1);

//taking polynomial terms from the user
for(i=0;i<=deg1;i++)
{

//entering values in coefficient of the polynomial terms
printf("\nEnter the coeff of x^%d :",i);
scanf("%f",&a[i].coeff);

//entering values in exponent of the polynomial terms
a[k++].exp = i;
}

//taking second polynomial from the user
printf("\nEnter the highest degree of poly2:");
scanf("%d",&deg2);

for(i=0;i<=deg2;i++)
{
printf("\nEnter the coeff of x^%d :",i);
scanf("%f",&b[i].coeff);

b[l++].exp = i;
}

//printing first polynomial
printf("\nExpression 1 = %.1f",a.coeff);
for(i=1;i<=deg1;i++)
{
printf("+ %.1fx^%d",a[i].coeff,a[i].exp);
}

//printing second polynomial
printf("\nExpression 2 = %.1f",b.coeff);
for(i=1;i<=deg2;i++)
{
printf("+ %.1fx^%d",b[i].coeff,b[i].exp);
}

if(deg1>deg2)
{
for(i=0;i<=deg2;i++)
{
c[m].coeff = a[i].coeff + b[i].coeff;
c[m].exp = a[i].exp;
m++;
}

for(i=deg2+1;i<=deg1;i++)
{
c[m].coeff = a[i].coeff;
c[m].exp = a[i].exp;
m++;
}

}
else
{
for(i=0;i<=deg1;i++)
{
c[m].coeff = a[i].coeff + b[i].coeff;
c[m].exp = a[i].exp;
m++;
}

for(i=deg1+1;i<=deg2;i++)
{
c[m].coeff = b[i].coeff;
c[m].exp = b[i].exp;
m++;
}
}

//printing the sum of the two polynomials
for(i=1;i<m;i++)
{
printf("+ %.1fx^%d",c[i].coeff,c[i].exp);
}

return 0;

}
```

Output
Enter the highest degree of poly1:5
Enter the coeff of x^0 :1
Enter the coeff of x^1 :2
Enter the coeff of x^2 :3
Enter the coeff of x^3 :4
Enter the coeff of x^4 :5
Enter the coeff of x^5 :6
Enter the highest degree of poly2:4
Enter the coeff of x^0 :4
Enter the coeff of x^1 :3
Enter the coeff of x^2 :2
Enter the coeff of x^3 :1
Enter the coeff of x^4 :0

Expression 1 = 1.0+ 2.0x^1+ 3.0x^2+ 4.0x^3+ 5.0x^4+ 6.0x^5
Expression 2 = 4.0+ 3.0x^1+ 2.0x^2+ 1.0x^3+ 0.0x^4
Expression after additon = 5.0+ 5.0x^1+ 5.0x^2+ 5.0x^3+ 5.0x^4+ 6.0x^5