C Program to add two polynomials using structures.

C Program to add two polynomials using structures. Structure is a c composite data type, in which we can define all the data types under the same name or object. Size of the Structure is the size of all data types, plus any internal padding. the key word “struct” is used to declare the structure. A Polynomial is a mathematical expression involving a sum of powers in one or more variables multiplied by coefficients. Read more about C Programming Language .

`/************************************************************ You can use all the programs on  www.c-program-example.com* for personal and learning purposes. For permissions to use the* programs for commercial purposes,* contact [email protected]* To find more C programs, do visit www.c-program-example.com* and browse!* *                      Happy Coding***********************************************************/#include < stdio.h >#include < conio.h >#define MAX 20struct addpolynomial { int exp, coef;};void main() { struct addpolynomial p1[MAX], p2[MAX], p3[MAX]; int max1, max2, max3; clrscr(); printf("nEnter first addpolynomial : "); max1 = read_addpolynomial(p1); printf("nEnter second addpolynomial : "); max2 = read_addpolynomial(p2); max3 = add_addpolynomial(p1, p2, p3, max1, max2); printf("nFirst addpolynomial is "); print_addpolynomial(p1, max1); printf("nSecond addpolynomial is "); print_addpolynomial(p2, max2); printf("n The resultant addpolynomial after addition is"); print_addpolynomial(p3, max3);}//function to read polynomialint read_addpolynomial(struct addpolynomial p[]) { int i, texp; i = 0; printf("nEnter exp ( use -1 to exit) : "); scanf("%d", &texp); while (texp != -1) {  p[i].exp = texp;  printf("nEnter coef : ");  scanf("%d", &p[i].coef);  i++;  printf("nEnter exp ( use -1 to exit) : ");  scanf("%d", &texp); } return (i);}//function to print polynomialint print_addpolynomial(struct addpolynomial p[], intMAX1) { int i; for (i = 0; i < max1; i++)  printf("%+dX%d ", p[i].coef, p[i].exp); return;}//function to ad polynomialsint add_addpolynomial( p1, p2, p3, max1, max2)struct addpolynomial p1[], p2[], p3[];intMAX1, max2;{ int i,j,k; i = j = k = 0; while ( i <max1 && j <max2) {  if( p1[i].exp > p2[j].exp)  {   p3[k] = p1[i];   k++;   i++;  }  else  if( p1[i].exp < p2[j].exp)  {   p3[k] = p2[j];   k++;   j++;  }  else  {   p3[k].exp = p1[i].exp;   p3[k].coef = p1[i].coef + p2[j].coef;   i++;   j++;   k++;  } } while( i <max1 ) {  p3[k] = p1[i];  k++;  i++; } while( j <max2 ) {  p3[k] = p2[j];  k++;  j++; } return(k);}`
`Array In CC Data StructuresSimple C Programs`

You can easily select the code by double clicking on the code area above.