Subtraction of Two-Variable Polynomial in C

Polynomials with two variables are common in engineering, graphics, and scientific computation. While addition is frequently discussed, subtraction is equally important when comparing polynomial expressions or solving equations.

What is a Two-Variable Polynomial?

A two-variable polynomial is an algebraic expression involving two variables, typically written in the form:

P(x, y) = a₁x^i y^j + a₂x^k y^l + ...

Each term has:

  • A coefficient
  • An exponent for x
  • An exponent for y
P1(x, y) = 3x²y + 4xy² + 2  
P2(x, y) = x²y + 2xy² + 1

Subtraction (P1 – P2):

(3x²y - x²y) + (4xy² - 2xy²) + (2 - 1) = 2x²y + 2xy² + 1

Structure of Polynomial in C

typedef struct {
    int coeff;
    int expX;
    int expY;
} Term;

C Program: Subtraction of Two Two-Variable Polynomials

#include <stdio.h>

#define MAX 100

typedef struct {
    int coeff;
    int expX;
    int expY;
} Term;

// Function to input polynomial
int inputPolynomial(Term poly[]) {
    int n;
    printf("Enter number of terms: ");
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        printf("Enter coefficient, exponent of x and exponent of y for term %d: ", i + 1);
        scanf("%d %d %d", &poly[i].coeff, &poly[i].expX, &poly[i].expY);
    }
    return n;
}

// Function to display polynomial
void displayPolynomial(Term poly[], int n) {
    for (int i = 0; i < n; i++) {
        printf("%d*x^%d*y^%d", poly[i].coeff, poly[i].expX, poly[i].expY);
        if (i < n - 1)
            printf(" + ");
    }
    printf("\n");
}

// Function to subtract polynomials: result = p1 - p2
int subtractPolynomials(Term p1[], int n1, Term p2[], int n2, Term result[]) {
    int i, j, k = 0;
    int used[MAX] = {0};

    for (i = 0; i < n1; i++) {
        int found = 0;
        for (j = 0; j < n2; j++) {
            if (p1[i].expX == p2[j].expX && p1[i].expY == p2[j].expY) {
                result[k].coeff = p1[i].coeff - p2[j].coeff;
                result[k].expX = p1[i].expX;
                result[k].expY = p1[i].expY;
                used[j] = 1;
                k++;
                found = 1;
                break;
            }
        }
        if (!found) {
            result[k] = p1[i];
            k++;
        }
    }

    // Add remaining terms of p2 with negative sign
    for (j = 0; j < n2; j++) {
        if (!used[j]) {
            result[k].coeff = -p2[j].coeff;
            result[k].expX = p2[j].expX;
            result[k].expY = p2[j].expY;
            k++;
        }
    }
    return k;
}

// Main function
int main() {
    Term poly1[MAX], poly2[MAX], result[MAX];
    int n1, n2, res;

    printf("Enter first polynomial\n");
    n1 = inputPolynomial(poly1);

    printf("Enter second polynomial\n");
    n2 = inputPolynomial(poly2);

    printf("\nFirst Polynomial: ");
    displayPolynomial(poly1, n1);

    printf("Second Polynomial: ");
    displayPolynomial(poly2, n2);

    res = subtractPolynomials(poly1, n1, poly2, n2, result);

    printf("\nResult (P1 - P2): ");
    displayPolynomial(result, res);

    return 0;
}

Output

Enter first polynomial
Enter number of terms: 2
Enter coefficient, exponent of x and exponent of y for term 1: 4 3 2
Enter coefficient, exponent of x and exponent of y for term 2: 5 3 2
Enter second polynomial
Enter number of terms: 2 
Enter coefficient, exponent of x and exponent of y for term 1: 6 3 2
Enter coefficient, exponent of x and exponent of y for term 2: 2 3 2

First Polynomial: 4*x^3*y^2 + 5*x^3*y^2
Second Polynomial: 6*x^3*y^2 + 2*x^3*y^2

Result (P1 - P2): -2*x^3*y^2 + -1*x^3*y^2 + -2*x^3*y^2
What did you think?

Similar Reads

Leave a Comment

Hi, Welcome back!
Forgot Password?
Don't have an account?  Register Now