Logo 
Search:

C++ Programming Articles

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

Program to multiply 2 polynomial functions

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

Write a Program to multiply 2 polynomial functions.

Code for Program to multiply 2 polynomial functions in C++ Programming

//multiplication of poly

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

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

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

void mul2poly :: mulpoly(){
      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=i;
      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=i;
      cout<<"Enter Co-efficient for degree"<<i<<"::  ";
      cin>>newl->coeff;
      newl->next=NULL;
      if(poly2==NULL)
         poly2=newl;
      else
         end->next=newl;
      end=newl;
      }

      //mul Logic
      poly *p1=poly1,*p2=poly2;
      int flag;
      end=NULL;
      while(p1 !=NULL){
    p2=poly2;
    while(p2!=NULL){
        //if(p1->pow == p2->pow){
            newl=new poly;
            newl->pow=p1->pow + p2->pow;
            newl->coeff=p1->coeff * p2->coeff;
            newl->next=NULL;
            if(poly3==NULL)
               poly3=newl;
            else{
               flag=1;

               //search if element already exist in poly3//flag will be zero if item already exist//1 otherwise
                 poly *tmp=poly3;
                 while(tmp!=NULL){
                   if(tmp->pow==newl->pow){
                    tmp->coeff += newl->coeff;
                    flag=0;
                   }
                   tmp=tmp->next;
                }

                //if item not present simply append it.if(flag==1)
                  end->next=newl;
            }
            end=newl;
            p2=p2->next;
        }
        p1=p1->next;
      }
}

void mul2poly :: 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();
    mul2poly obj;
    obj.mulpoly();
    obj.display();
    getch();
}
  
Share: 


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

Easy Tutor
Easy Tutor author of Program to multiply 2 polynomial functions 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].

 
No Comment Found, Be the First to post comment!