# C Program Code for Addition of Two Polynomials using Arrays

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

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 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 pre-dominates.

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

## 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

## Results of polynomial addition

- The Concept of Operator Overloading in C++

Operator overloading concept gives a special meaning to an operator such as ‘+’ or ‘-‘. Addition or subtraction cannot be directly performed with objects as we do with basic data types. Like overloading functions, operators also can be overloaded for - C++ Program Code for Polynomial Addition Using Binar...

Even if the logic involved for polynomial addition in C++ is same as that of C, the main advantage with C++ is that both the polynomials can be added directly with the concept of operator overloading.

**© 2013 Radhika Sreekanth**

## Comments

**sanjay paudel** on December 25, 2018:

your idea is really awesome author ..

How can i follow ask other question??

**العالمية كلين** from Saudi Arabia on December 18, 2016:

hey radhikasree

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

**Radhika Sreekanth (author)** from Mumbai,India on November 23, 2016:

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

**Ryan Dsouza** on October 14, 2016:

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

**sangeetha** on August 30, 2016:

Thank you mam.

**Radhika Sreekanth (author)** from Mumbai,India on May 24, 2015:

Hi dharam,

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

**Radhika Sreekanth (author)** from Mumbai,India on May 22, 2015:

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.

**dharam** on May 19, 2015:

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.

**Garima** on May 19, 2015:

how can we do the same using both arrays and structures

**Radhika Sreekanth (author)** from Mumbai,India on April 21, 2015:

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.

**priyanka** on April 20, 2015:

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

**Radhika Sreekanth (author)** from Mumbai,India on November 16, 2014:

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

**Sourath** on November 15, 2014:

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

**mammootty** on March 01, 2014:

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

**Radhika Sreekanth (author)** from Mumbai,India on September 21, 2013:

Thanks jeni!

**jeni...** on September 17, 2013:

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

**Radhika Sreekanth (author)** from Mumbai,India on September 13, 2013:

@Himanshu

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

**Himanshu** on September 12, 2013:

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

**Radhika Sreekanth (author)** from Mumbai,India on March 06, 2013:

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.

**atein** on March 06, 2013:

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 ?

**Radhika Sreekanth (author)** from Mumbai,India on March 05, 2013:

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.

**Ruby Jean Richert** from Southern Illinois on March 05, 2013:

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.