Monday 24 October 2011

Program on add and multiplication of Polynomials


#include<iostream>
using namespace std;
#define n 100
class poly
{
 private:
                int a[n], b[n], add[n], mul[n], p, q, at;
        public:
                void init ( );
void input ( );
                void process ( );
                void display ( );
};
void poly :: init ( )
{
        int i;
        for( i = 0; i < n; i++)
                a[ i ] = b [ i ] = add[ i ] = mul[ i ] = 0;
}
void poly :: input ( )
{
        int i;
    cout<<"\nEnter Degree Of First Polynomial::";
        cin>>p;
        cout<<"\nEnter Degree Of Second Polynomial::";
        cin>>q;
        cout<<"\nEnter Values in First Polynomial\n";
        for( i = 0; i <= p; i++)
        {
                cout<<"\nEnter X^"<<i<<" The Coefficient";
                cin>>a[ i ];
        }
        cout<<"\nEnter Values in Second Polynomial\n";
        for( i = 0; i <= q; i++)
        {
                cout<<"\nEnter X^"<<i<<" The Coefficient";
                cin>>b[ i ];
        }
}
void poly :: process ( )
{
        int i, j;
        if( p > q )
                at = p;
        else
                at = q;
        for ( i = 0; i <= at;i++   )
                add[ i ] = a[ i ] + b[ i ];
                for( i = 0;  i <= p;  i++)
                        for( j = 0; j <= q; j++)
                                  mul[i+j]+= a[i]*b[j];
}
void poly :: display ( )
{
 int i;
cout<<"Addition Of Two Polynomial Expressions Are\n\n";
for( i = at; i >=0 ; i--)
cout<<add[i]<<"X^"<<i<<"+";
cout<<"\n\nMultiplication Of Two Polynomial Expressions Are\n\n";
for( i = p + q; i >= 0; i--)
cout<<mul[i]<<"X^"<< i <<"+";
}
int main()
{
poly ob;
ob.init ( );
ob.input ( );
ob.process ( );
ob.display ( );
return 0;
}


OUTPUT:-

Enter Degree Of First Polynomial::2

Enter Degree Of Second Polynomial::2

Enter Values in First Polynomial

Enter X^0 The Coefficient1

Enter X^1 The Coefficient2

Enter X^2 The Coefficient5

Enter Values in Second Polynomial

Enter X^0 The Coefficient6

Enter X^1 The Coefficient4

Enter X^2 The Coefficient7
Addition Of Two Polynomial Expressions Are

12X^2+6X^1+7X^0+

Multiplication Of Two Polynomial Expressions Are

35X^4+34X^3+45X^2+16X^1+6X^0+

No comments:

Post a Comment