Write a ‘C’ program to accept two polynomials and find the Multiplication of accepted polynomials.
Polynomial Multiplication in C
Mathematical expressions known as polynomials are made up of variables raised to different powers and multiplied by coefficients. They are extensively used in many different disciplines, including as computer science, engineering, physics, and mathematics. When multiplying polynomials, terms from one polynomial are multiplied by terms from another polynomial, and then like terms are combined to produce the result. We will look at how to create a "C" programme that can accept two polynomials and determine their multiplication in this post.
Polynomial Multiplication: An Overview
Let's have a look at two polynomials:
P(x) = a(sub>0/sub> + a(sub>1/sub>x + a(sub>2/sub>x +... + a(sub>n/sub>x + a(sub>2/sub>x + a(sub>2/sub>xsup>2/sup>)
Q(x) = b(0,1) + b(1,x), b(2,x), b(2,x),... + b(m,x), b(m,x), b(m,x), b(m,x), b(m,x), b(m,x), b(m,x), b(m,x), b(m,x), b(m,x), b(m,x), b(
These two polynomials' product, R(x), will have terms that take the following form:
R<sub>k</sub>(x) = a(i, j), b(j, x)(i, j), and i+j (i+j).
'C' Programme
#include <stdio.h>
// Function to multiply two polynomials and store the result in the third polynomial
void multiplyPolynomials(int poly1[], int poly2[], int n1, int n2, int resultPoly[]) {
int i, j;
// Initialize the result polynomial to have all coefficients as 0
for (i = 0; i < n1 + n2 + 1; i++) {
resultPoly[i] = 0;
}
// Perform polynomial multiplication
for (i = 0; i <= n1; i++) {
for (j = 0; j <= n2; j++) {
resultPoly[i + j] += poly1[i] * poly2[j];
}
}
}
// Function to display the polynomial in a readable format
void displayPolynomial(int poly[], int n) {
for (int i = 0; i < n; i++) {
if (i == n - 1) {
printf("%dx^%d", poly[i], i);
} else {
printf("%dx^%d + ", poly[i], i);
}
}
printf("\n");
}
int main() {
int n1, n2;
printf("Enter the degree of the first polynomial: ");
scanf("%d", &n1);
int poly1[100];
printf("Enter the coefficients of the first polynomial terms from highest to lowest degree:\n");
for (int i = n1; i >= 0; i--) {
scanf("%d", &poly1[i]);
}
printf("Enter the degree of the second polynomial: ");
scanf("%d", &n2);
int poly2[100];
printf("Enter the coefficients of the second polynomial terms from highest to lowest degree:\n");
for (int i = n2; i >= 0; i--) {
scanf("%d", &poly2[i]);
}
int resultDegree = n1 + n2;
int resultPoly[100];
multiplyPolynomials(poly1, poly2, n1, n2, resultPoly);
printf("First polynomial: ");
displayPolynomial(poly1, n1 + 1);
printf("Second polynomial: ");
displayPolynomial(poly2, n2 + 1);
printf("Multiplication of the two polynomials: ");
displayPolynomial(resultPoly, resultDegree + 1);
return 0;
}