Write a ‘C’ program to accept two polynomials and find the Multiplication of accepted polynomials.

 

c programs

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;
}




Workings of the Programme
The user is first asked to enter the degrees and coefficients of the two input polynomials.

The degree of the resulting polynomial is then determined as the sum of the degrees of the input polynomials less one.

The coefficients of the resulting polynomial are initially stored in a brand-new array called result. It starts out with zeros.

The multiplication is carried out by the nested loops, which iterate over each term of the input polynomials as previously mentioned. The corresponding places in the result array are updated with the results.

The programme then shows the coefficients of the final polynomial, which is the result of combining the two input polynomials.


Conclusion
A fundamental mathematical operation with numerous applications is polynomial multiplication. We may improve our programming abilities and gain a better understanding of the underlying mathematical ideas by creating a "C" programme that multiplies two polynomials. You can make a programme that accurately calculates the multiplication of two polynomials by using the procedures described in this article.


Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

You are being redirected...

10