ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming

C Program Code for Addition of Two Polynomials using Arrays

Updated on August 11, 2017
radhikasree profile image

Radhika has the degree of master in computer applications. She has worked as a faculty with Aptech Computer Education Center for sometime.

Polynomial addition
Polynomial addition | Source

Polynomials come under the section Algebra in Mathematics. A polynomial is an expression of finite length constructed from variables, constants and non-negative 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 3x2+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.

  1. 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

5x3+2x2+7

7x3+9x2+12

-------------------

12x3+11x2+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 pre-dominates.

9x4+5x3+ +2x

3x4+ +4x2+7x

------------------------

12x4+5x3+4x2+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 time-consuming, 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

First part of polynomial addition
First part of polynomial addition | Source

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 1st 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 1st polynomial. Similarly degree also has to be equated correspondingly. Then, no. of terms of 1st 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

Second part of polynomial addition
Second part of polynomial addition | Source

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

Third part of polynomial addition
Third part of polynomial addition | Source
Addition of polynomials when the no. of terms and corresponding degrees differ
Addition of polynomials when the no. of terms and corresponding degrees differ | Source

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 co-efficients:");

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

Addition of polynomials when the no. of terms and degrees of corresponding terms are equal
Addition of polynomials when the no. of terms and degrees of corresponding terms are equal | Source

Do you find this code to be complex?

See results

© 2013 Radhika Sreekanth

Comments

    0 of 8192 characters used
    Post Comment

    • alaamiahclean profile image

      العالمية كلين 8 months ago from Saudi Arabia

      hey radhikasree

      Great blog.Really looking forward to read more. Cool.

    • radhikasree profile image
      Author

      Radhika Sreekanth 9 months ago from Mumbai,India

      Thanks Ryan Dsouza for that golden comment. Your comment is inspiring me to create more such content.

    • profile image

      Ryan Dsouza 10 months ago

      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. :)

    • profile image

      sangeetha 11 months ago

      Thank you mam.

    • profile image

      sunil gavaskar 17 months ago

      hi mam hw r u

    • radhikasree profile image
      Author

      Radhika Sreekanth 2 years ago from Mumbai,India

      Hi dharam,

      Nice to hear that you own a blog about computer programming. I"ll visit it for sure. Thanks for commenting.

    • radhikasree profile image
      Author

      Radhika Sreekanth 2 years ago from Mumbai,India

      Hi Garima,

      Declare a structure named poly and let a[] and b[] be the data members storing degrees and co-efficients of the polynomials respectively. Then if x and y are two variables of struct 'poly' type, do the addition by accessing the variables as x.a[], x.b[], y.a[] and y.b[]. Hope you got it.

    • profile image

      dharam 2 years ago

      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.

    • profile image

      Garima 2 years ago

      how can we do the same using both arrays and structures

    • radhikasree profile image
      Author

      Radhika Sreekanth 2 years ago from Mumbai,India

      Hi Priyanka,

      Without seeing the program I can't answer your query. They are not keywords and hope they are inserted in '//' to describe the relevant step of the program.

    • profile image

      priyanka 2 years ago

      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

    • radhikasree profile image
      Author

      Radhika Sreekanth 2 years ago from Mumbai,India

      Hi Sourath,

      Appreciate your question.

      A program is one that is crafted according to some particular requirements and it works best for that criteria only. So if the program asks you to input degree in decreasing order, you should feed it in that manner itself to get results. Else the program may crash as it is not programmed according to your likes.

      If you wish, a validation can be included that says, "Input data don't match the program format".

    • profile image

      Sourath 2 years ago

      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

    • profile image

      mammootty 3 years ago

      thank u polynomial addition is very simple written than me.any ways thanks a lot.

    • radhikasree profile image
      Author

      Radhika Sreekanth 3 years ago from Mumbai,India

      Thanks jeni!

    • profile image

      jeni... 3 years ago

      this program is very easily to study.....

    • radhikasree profile image
      Author

      Radhika Sreekanth 3 years ago from Mumbai,India

      @Himanshu

      Algorithm for multiplication would take time to develop as the entire logic is different. Give me some time, then I'll post it.

    • profile image

      Himanshu 3 years ago

      HOW to multiply of polynomial equation , plz include c code with algo...

    • radhikasree profile image
      Author

      Radhika Sreekanth 4 years ago from Mumbai,India

      Thanks atein for reading and voting. Yes, we can do it using structures simply or even by using linked lists. That may shorten the code length too.

    • profile image

      atein 4 years ago

      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 ?

    • radhikasree profile image
      Author

      Radhika Sreekanth 4 years ago from Mumbai,India

      Hi Ruby,

      Yes, this is going to be useful for students of Computer programming. I've worked as a Faculty with Aptech Computer Education Centre in the past. Thanks for reading and trying it. Take care.

    • always exploring profile image

      Ruby Jean Fuller 4 years ago from Southern Illinois

      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.