Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Mathematics ProgramRSS Feeds

Program to addition of two polynomial

Posted By: Easy Tutor     Category: C++ Programming     Views: 16997

Write a Program to addition of two polynomial.

Code for Program to addition of two polynomial in C++ Programming

//Addition of Two Polynomial

#include <iostream.h>
#include <iomanip.h>
#include <conio.h>

struct poly{
    int coeff;
    int pow;
    poly *next;
};

class add2poly
{
   poly *poly1, *poly2, *poly3;
   public:
   add2poly(){poly1=poly2=poly3=NULL;}
   void addpoly();
   void display();
};

void add2poly :: addpoly(){
      int i,p;
      poly *newl=NULL,*end=NULL;
      cout<<"Enter highest power for x\n";
      cin>>p;
    //Read first poly
      cout<<"\nFirst Polynomial\n";
      for(i=p;i>=0;i--)
      {
      newl=new poly;
      newl->pow=p;
      cout<<"Enter Co-efficient for degree"<<i<<"::  ";
      cin>>newl->coeff;
      newl->next=NULL;
      if(poly1==NULL)
         poly1=newl;
      else
         end->next=newl;
      end=newl;
      }

    //Read Second poly
      cout<<"\n\nSecond Polynomial\n";
      end=NULL;
      for(i=p;i>=0;i--)
      {
      newl=new poly;
      newl->pow=p;
      cout<<"Enter Co-efficient for degree"<<i<<"::  ";
      cin>>newl->coeff;
      newl->next=NULL;
      if(poly2==NULL)
         poly2=newl;
      else
         end->next=newl;
      end=newl;
      }

      //Addition Logic
      poly *p1=poly1,*p2=poly2;
      end=NULL;
      while(p1 !=NULL && p2!=NULL){
        if(p1->pow == p2->pow){
            newl=new poly;
            newl->pow=p--;
            newl->coeff=p1->coeff + p2->coeff;
            newl->next=NULL;
            if(poly3==NULL)
               poly3=newl;
            else
               end->next=newl;
            end=newl;
        }
        p1=p1->next;
        p2=p2->next;
      }
}

void add2poly :: display(){
   poly *t=poly3;
   cout<<"\n\nAnswer after addition is : ";
   while(t!=NULL){
      cout.setf(ios::showpos);
      cout<<t->coeff;
      cout.unsetf(ios::showpos);
      cout<<"X"<<t->pow;
      t=t->next;
   }
}


void main(){
    clrscr();
    add2poly obj;
    obj.addpoly();
    obj.display();
    getch();
}
  
Share: 


Didn't find what you were looking for? Find more on Program to addition of two polynomial Or get search suggestion and latest updates.

Easy Tutor
Easy Tutor author of Program to addition of two polynomial is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
Monica Kala from United States Comment on: Oct 10
plz..suggest a code for polynomial subtraction in c(Data structures)

View All Comments