C Program Code for Addition of Two Polynomials using Arrays
Polynomials come under the section Algebra in Mathematics. A polynomial is an expression of finite length constructed from variables, constants and nonnegative integer exponents. The operations addition, subtraction and multiplication determine its entire structure. Polynomials are used in a wide variety of problems where they are called as polynomial equations.
An example of a polynomial is 3x^{2}+2x+7; here, the total number of terms is 3. The coefficients of each term are 3, 2, 7 and degrees 2, 1, 0 respectively.
While adding two polynomials, following cases need to be considered.
 When the degrees of corresponding terms of the two polynomials are same:
This is the normal case when corresponding coefficients of each term can be added directly. For example, the sum of the polynomials
5x^{3}+2x^{2}+7
7x^{3}+9x^{2}+12

12x^{3}+11x^{2}+19 is a simple addition where all the degrees of the corresponding terms are same.
2. When the degrees of corresponding terms of the polynomials are different:
Here, the term with the larger degree predominates.
9x^{4}+5x^{3}+ +2x
3x^{4}+ +4x^{2}+7x

12x^{4}+5x^{3}+4x^{2}+9x
Algorithm
Here, I’m writing the program for polynomial addition in C language using arrays and as printing a polynomial in its form is a little timeconsuming, the code also got lengthier.
Let ‘m’ and ‘n’ be the no. of terms of the two polynomials represented by arrays a[] and b[]. Sum of them is represented by c[] and i, j, k respectively are the subscripts used to denote the various elements of arrays a, b and c. The logic of the program is written in 3 parts considering each possibility in order to obtain the exact sum.
First part
Variables i, j, k are set to zero in the beginning. The condition that ‘m’ and ‘n’ should be greater than zero is what is required for execution of the first part. Then, the next checking condition that a[i] equal to b[j] determines whether the degree of the first term of the first polynomial is equal to the degree of the first term of the second polynomial. If this is true, then the sum of the coefficients a[i+1] and b[j+1] is stored in c[k+1] and the degree a[i] or b[i] (both same) in c[k]. As, summation takes place with one term from each of the polynomials, each term gets reduced by 1. Also, the subscripts ‘i’ and ‘j’ are moved forward by 2 as each term has a degree and a coefficient.
If the checking condition becomes false, then next check a[i] greater than b[j] finds out whether the degree of the first polynomial is greater than that of the second. If this is true, then the degree of the sum c[k] is set to a[i] and the coefficient c[k+1] to a[i+1]. The no. of terms of first polynomial gets reduced by 1 and the subscript ‘i’ also moved forward by 2.If this condition becomes false, then the degree of the sum c[k] becomes b[j] and c[k+1]=b[j+1]. The no. of terms of second polynomial gets reduced by 1 and the subscript ‘j’ is moved forward by 2.
After executing just one of these possible cases, control finally reaches the statement k=k+2 that moves forward the subscript of array ‘c’ by 2. When either ‘m’ or ‘n’ becomes zero, control exits the loop and moves to the second part.
Flowchart of first part
Second part
As there can be some terms remaining in either first polynomial or second after the execution of first part, they need to be added to the polynomial sum. This is done by iterating a set of statements for the condition that the no. of terms is greater than zero taking each independently.
The no. of terms of 1^{st }polynomial is checked first. If it is greater than zero, then the coefficient of polynomial sum where it is standing at present is equated to the current coefficient of the 1^{st} polynomial. Similarly degree also has to be equated correspondingly. Then, no. of terms of 1^{st }polynomial gets reduced by 1 and subscripts ‘i’ and ‘k’ skip forward by 2. The loop is executed as long as the condition is true.
Flowchart of second part
Third part
The second part is repeated here for second polynomial. After all the remaining terms of it gets added to the sum, the control exits the loop and final result is obtained.
Flowchart of third part
C program code
#include<stdio.h> #include<conio.h> main() { int a[10], b[10], c[10],m,n,k,k1,i,j,x; clrscr(); printf("\n\tPolynomial Addition\n"); printf("\t===================\n"); printf("\n\tEnter the no. of terms of the polynomial:"); scanf("%d", &m); printf("\n\tEnter the degrees and coefficients:"); for (i=0;i<2*m;i++) scanf("%d", &a[i]); printf("\n\tFirst polynomial is:"); k1=0; if(a[k1+1]==1) printf("x^%d", a[k1]); else printf("%dx^%d", a[k1+1],a[k1]); k1+=2; while (k1<i) { printf("+%dx^%d", a[k1+1],a[k1]); k1+=2; } printf("\n\n\n\tEnter the no. of terms of 2nd polynomial:"); scanf("%d", &n); printf("\n\tEnter the degrees and coefficients:"); for(j=0;j<2*n;j++) scanf("%d", &b[j]); printf("\n\tSecond polynomial is:"); k1=0; if(b[k1+1]==1) printf("x^%d", b[k1]); else printf("%dx^%d",b[k1+1],b[k1]); k1+=2; while (k1<2*n) { printf("+%dx^%d", b[k1+1],b[k1]); k1+=2; } i=0; j=0; k=0; while (m>0 && n>0) { if (a[i]==b[j]) { c[k+1]=a[i+1]+b[j+1]; c[k]=a[i]; m; n; i+=2; j+=2; } else if (a[i]>b[j]) { c[k+1]=a[i+1]; c[k]=a[i]; m; i+=2; } else { c[k+1]=b[j+1]; c[k]=b[j]; n; j+=2; } k+=2; } while (m>0) { c[k+1]=a[i+1]; c[k]=a[i]; k+=2; i+=2; m; } while (n>0) { c[k+1]=b[j+1]; c[k]=b[j]; k+=2; j+=2; n; } printf("\n\n\n\n\tSum of the two polynomials is:"); k1=0; if (c[k1+1]==1) printf("x^%d", c[k1]); else printf("%dx^%d", c[k1+1],c[k1]); k1+=2; while (k1<k) { if (c[k1+1]==1) printf("+x^%d", c[k1]); else printf("+%dx^%d", c[k1+1], c[k1]); k1+=2; } getch(); return 0; }
Results of polynomial addition
Do you find this code to be complex?
© 2013 Radhika Sreekanth
Comments
your idea is really awesome author ..
How can i follow ask other question??
hey radhikasree
Great blog.Really looking forward to read more. Cool.
Dear author,
The explanation you have provided is simply golden.... Amazing clarity, precise explanation and an easy to follow explanation..
Thanks your creating such quality content. :)
Thank you mam.
Thank you very much for this article... I really enjoy reading your articles. I am a software engineer with 7+ years of experience and I author a blog which is for computer programming students. I really got inspiration from your blog. Here is a link to my blog please have a look http://techieme.in
I was trying to explore the possibility of getting few links to my blog from here. Please let me know if you are open to that..
Thanks a tonne.
how can we do the same using both arrays and structures
hello mam,
here i have a doubt
why we are using match,proceed words in add of poly program
and what these words represents
plz give reply to my quest


What happens if someone doesn't inputs the polynomial degree in decreasing order.I mean if someone enters the polynomial as both increasing and decreasing order of degree
1st polynomial as : 3x^6+5x^4+2x^2+1
2nd polynomial as :2+7x^2+3x^4+2x^6
thank u polynomial addition is very simple written than me.any ways thanks a lot.
this program is very easily to study.....
HOW to multiply of polynomial equation , plz include c code with algo...
Good hub . Voted up . Nice Flowchart/ Algo . We could have done the same using structures . I mean we can also use structures in C to add polynomials . Right ?
I am sure this is useful to many who study this type of mathematics, but for me, i am lost, but i have a hard time with any kind of math. I was able to work out one part after you explained the technique. Thank's for sharing. Are you a teacher? Cheers.
22