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
-
Multiplications of Two variable polynomial
Polynomial multiplication is a common operation in algebra, used in areas such as scientific computing, engineering simulations, and symbolic algebra… -
Explain the Addition of Two variable polynomial
Polynomials are fundamental in mathematics, and their use extends into computer science, engineering, physics, and more. While single-variable polynomials are… -
Reverses the Doubly Circular Linked List
Algorithm to Reverse a Doubly Circular Linked List Check if the List is Empty or Has Only One Node: If… -
Algorithm and Program in C to traverse Doubly Circular Linked List
A doubly circular linked list is a special type of linked list in which every node is connected to both… -
Polynomial representation Using Array
Polynomials are fundamental mathematical expressions used extensively in various fields. Representing them efficiently in programming is crucial for calculations and… -
Multiplication of Single Variable Polynomial : Algorithm and Program
Multiplication of single-variable polynomials is a fundamental operation in polynomial algebra. The process involves multiplying each term of one polynomial… -
Subtraction of Single variable Polynomial : Algorithm and Program
Subtracting single-variable polynomials involves reducing each corresponding term of the polynomials from each other. This process is similar to addition,… -
Addition of Single variable Polynomial : Program and Algorithm
The addition of single-variable polynomials is a fundamental concept in algebra, often encountered in mathematics and computer science. Let's break… -
Polynomial Representation Using Linked List
Polynomial representation using linked lists is a critical concept in computer science and mathematics. In this guide, we explore how… -
Delete a Node at End of Doubly Circular Linked List
In this tutorial we will learn writing Algorithm and Program to delete a node from beginning on Doubly Circular Linked…