top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to add two polynomials in C or C++?

0 votes
296 views
How to add two polynomials in C or C++?
posted Jan 15, 2015 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

We represent polynomials by the linked list and perform addition.

void add(struct node *a,struct node *b,struct node **s)
{
        struct node *temp;

    if(a==NULL&&b==NULL)
    return;

    while(a!=NULL&&b!=NULL)
    {
             if(*s==NULL)
             {
                 *s=malloc(sizeof(struct node));
                 temp=*s;
             }
             else
             {
                 temp->link=malloc(sizeof(struct node));
                 temp=temp->link;
             }
            if(a->exp<b->exp)
            {
                temp->coeff=b->coeff;
                temp->exp=b->exp;
                b=b->link;
            }
            else if(a->exp>b->exp)
            {
                    temp->coeff=a->coeff;
                    temp->exp=a->exp;
                    a=a->link;
            }
            else if(a->exp==b->exp)
           {
                temp->coeff=a->coeff+b->coeff;
                temp->exp=a->exp;
                a=a->link;
                b=b->link;
           }

    }

}

answer Jan 16, 2015 by Chirag Jain
Similar Questions
+1 vote

I want to add two 64-bit or more than 64-bit numbers and print sum which is of 64-bit or more than 64-bit.

0 votes

How we can add two 64 bit number on 32 bit machine using c/c++ program ?

+3 votes
...