Subtraction of two-variable polynomials in C is a process of finding the difference between two polynomials that have terms with variables like x and y. In simple terms, it involves matching terms with the same exponents and subtracting their coefficients. Each term of the first polynomial is compared with the terms of the second polynomial, and the result is stored in a new polynomial. This allows us to represent the difference of the two polynomials clearly and perform further calculations if needed.
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
Explanation of Code
Header Files and Definitions
#include <stdio.h>
#define MAX 100
The above program includes stdio.h to use standard input/output functions like printf and scanf. The line #define MAX 100 sets a constant value of 100, which can be used as the maximum number of terms allowed in a polynomial or as the size of arrays used in the program.
Polynomial Term Structure
typedef struct {
int coeff;
int expX;
int expY;
} Term;
The above code defines a new data type called Term using typedef struct. Each Term represents a single term of a polynomial with three fields: coeff for the coefficient, expX for the exponent of x, and expY for the exponent of y. This structure allows the program to store and manage polynomial terms efficiently.
Input a 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;
}
The above function inputPolynomial is used to read a polynomial from the user and store it in an array of Term structures. It first asks the user to enter the number of terms in the polynomial. Then, for each term, it prompts the user to input the coefficient, exponent of x, and exponent of y, storing these values in the corresponding fields of the array. Finally, it returns the total number of terms entered, so the program knows how many terms the polynomial contains.
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");
}
The above function displayPolynomial is used to print a polynomial stored in an array of Term structures. It loops through each term and prints it in the format coeff*x^expX*y^expY. After each term except the last one, it prints a + sign to separate the terms. Finally, it prints a newline to complete the display of the polynomial.
Subtract Polynomials
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]; // term exists only in P1
k++;
}
}
// Add remaining terms of p2 with negative sign
for (j = 0; j < n2; j++) {
if (!used[j]) {
result[k].coeff = -p2[j].coeff; // subtract P2 term
result[k].expX = p2[j].expX;
result[k].expY = p2[j].expY;
k++;
}
}
return k;
}
The above function subtractPolynomials is used to subtract the second polynomial p2 from the first polynomial p1 and store the result in the result array. It goes through each term of p1 and checks if there is a term in p2 with the same exponents. If a matching term is found, their coefficients are subtracted and stored in the result, while the corresponding term in p2 is marked as used. If no matching term is found, the term from p1 is copied directly to the result. After processing all terms of p1, any remaining terms from p2 that were not used are added to the result with their coefficients negated. The function then returns the total number of terms in the resulting polynomial.
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;
}
The main function demonstrates subtraction of two polynomials using the previously defined functions. It first declares arrays poly1, poly2, and result to store the terms of the two input polynomials and their difference. It then reads the first and second polynomials from the user using inputPolynomial and displays them using displayPolynomial. Next, it calls subtractPolynomials to compute poly1 - poly2 and stores the result in the result array. Finally, it displays the resulting polynomial and ends the program.
Time & Space Complexity
| Function | Time Complexity |
|---|---|
inputPolynomial | O(n) |
displayPolynomial | O(n) |
subtractPolynomials | O(n1 * n2) |
main | O(n1 + n2 + n1*n2) |
| Function / Component | Space Complexity |
|---|---|
poly1, poly2, result | O(MAX) each → O(MAX) overall |
used[] array in subtraction | O(MAX) |
Local variables (i, j, k, n1, n2, res) | O(1) |
| Total | O(MAX) |