C Program To Multiply Two Polynomials

How to in Write a  C program to multiply two polynomials in C Programming Language ?


Solution:
/*
Write a C program to multiply two polynomials.
*/


#include< stdio.h>
#include< conio.h>

struct poly
{
int degree;
int coeff;
};/*End of structure definition*/

void main()
{
struct poly poly1[10],poly2[10],product[100];
int noOfTerms1,noOfTerms2,count=-1;
int i,j;
clrscr();
printf("\nEnter Number Of Terms Of 1st Polynomial: ");
scanf("%d",&noOfTerms1);
for(i=0;i< noOfTerms1;i++)
{
printf("\nEnter Degree: ");
scanf("%d",&poly1[i].degree);
printf("\nEnter Coefficient: ");
scanf("%d",&poly1[i].coeff);
}/*End of i for loop*/
printf("\nEnter Number Of Terms Of 2nd Polynomial: ");
scanf("%d",&noOfTerms2);
for(i=0;i< noOfTerms2;i++)
{
printf("\nEnter Degree: ");
scanf("%d",&poly2[i].degree);
printf("\nEnter Coefficient: ");
scanf("%d",&poly2[i].coeff);
}/*End of i for loop*/
for(i=0;i< noOfTerms1;i++)
{
for (j=0;j< noOfTerms2;j++)
{
product[++count].degree=poly1[i].degree+poly2[j].degree;
product[count].coeff=poly1[i].coeff*poly2[j].coeff;
}/*End of j for loop*/
}/*End of i for loop*/
printf("\nThe Product Of Two Polynomials Is: \n");
for(i=0;i<=count;i++)
{
if(product[i].degree==0)
printf("%d ",product[i].coeff);
else if(product[i].degree==1)
printf("%dx ",product[i].coeff);
else
{
printf("%dx^%d ",product[i].coeff,product[i].degree);
}
if(i!=count)
printf("+ ");
}/*End of i for loop*/
getch();
}/*End of main()*/
/*
Output:

Enter Number Of Terms Of 1st Polynomial: 3

Enter Degree: 4

Enter Coefficient: 3

Enter Degree: 3

Enter Coefficient: 6

Enter Degree: 2

Enter Coefficient: 4

Enter Number Of Terms Of 2nd Polynomial: 3

Enter Degree: 5

Enter Coefficient: 7

Enter Degree: 4

Enter Coefficient: 2

Enter Degree: 1

Enter Coefficient: 2

The Product Of Two Polynomials Is:
21x^9 + 6x^8 + 6x^5 + 42x^8 + 12x^7 + 12x^4 + 28x^7 + 8x^6 + 8x^3
*/


Learn More :