ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

C Program Code for Addition of Two Polynomials using Arrays

Updated on September 13, 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

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

Do you find this code to be complex?

See results

© 2013 Radhika Sreekanth

Comments

    0 of 8192 characters used
    Post Comment

    • alaamiahclean profile image

      العالمية كلين 

      24 months ago from Saudi Arabia

      hey radhikasree

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

    • radhikasree profile imageAUTHOR

      Radhika Sreekanth 

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

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

      2 years ago

      Thank you mam.

    • radhikasree profile imageAUTHOR

      Radhika Sreekanth 

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

      Radhika Sreekanth 

      3 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 

      3 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 

      3 years ago

      how can we do the same using both arrays and structures

    • radhikasree profile imageAUTHOR

      Radhika Sreekanth 

      3 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 

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

      Radhika Sreekanth 

      4 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 

      4 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 

      4 years ago

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

    • radhikasree profile imageAUTHOR

      Radhika Sreekanth 

      5 years ago from Mumbai,India

      Thanks jeni!

    • profile image

      jeni... 

      5 years ago

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

    • radhikasree profile imageAUTHOR

      Radhika Sreekanth 

      5 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 

      5 years ago

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

    • radhikasree profile imageAUTHOR

      Radhika Sreekanth 

      5 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 

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

      Radhika Sreekanth 

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

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

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://hubpages.com/privacy-policy#gdpr

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)