0
Explore
0

Explain the Addition of Two variable polynomial

Updated on October 6, 2025

Polynomials are basic mathematical expressions made up of terms with coefficients and variables. They are widely used in fields like computer science, engineering, and physics to model different kinds of problems. While single-variable polynomials involve only one variable, two-variable polynomials use two variables, like x and y, which makes them more flexible. They can represent more complex relationships and patterns in multidimensional space, helping to solve problems that depend on more than one factor.

What is a Two-Variable Polynomial?

A two-variable polynomial is a mathematical expression involving two variables, typically x and y, with non-negative integer exponents.

Example:

P(x, y) = 3x²y + 4xy² + 5
Q(x, y) = 2x²y + 6xy² + 7

When adding P(x, y) and Q(x, y), we combine like terms (same powers of x and y):

Result = (3+2)x²y + (4+6)xy² + (5+7)
       = 5x²y + 10xy² + 12

Understanding Like Terms

  • Like terms have identical exponents for both x and y.
  • Only like terms can be combined by summing their coefficients.

For instance:

  • 3x²y and 2x²y are like terms.
  • 3xy² and 4x²y are not like terms.

How to Represent a Polynomial in C?

To store and manipulate polynomials programmatically, we define each term using a structure that includes:

  • coefficient
  • exponent of x (expX)
  • exponent of y (expY)
struct Term {
    int coeff;
    int expX;
    int expY;
};

The entire polynomial is then represented as an array of Term.

#include <stdio.h>
#include <stdlib.h>

// Define structure for each term in the polynomial
typedef struct {
    int coeff;
    int expX;
    int expY;
} Term;

// Function to input a polynomial
void inputPolynomial(Term poly[], int 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);
    }
}

// Function to add two polynomials
int addPolynomials(Term p1[], int n1, Term p2[], int n2, Term result[]) {
    int i, j, k = 0;
    int used[n2];
    for (i = 0; i < n2; i++) used[i] = 0;

    // Combine like terms
    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;
                found = 1;
                k++;
                break;
            }
        }
        if (!found) {
            result[k++] = p1[i];
        }
    }

    // Add remaining terms from second polynomial
    for (j = 0; j < n2; j++) {
        if (!used[j]) {
            result[k++] = p2[j];
        }
    }

    return k; // number of terms in the result
}

// Function to display a 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);
    }
    printf("\n");
}

// Main function
int main() {
    int n1, n2;
    printf("Enter number of terms in first polynomial: ");
    scanf("%d", &n1);
    Term poly1[n1];
    inputPolynomial(poly1, n1);

    printf("Enter number of terms in second polynomial: ");
    scanf("%d", &n2);
    Term poly2[n2];
    inputPolynomial(poly2, n2);

    Term result[n1 + n2]; // max possible size
    int resSize = addPolynomials(poly1, n1, poly2, n2, result);

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

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

    printf("Sum of Polynomials:\n");
    displayPolynomial(result, resSize);

    return 0;
}

Output

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

First Polynomial:
+1*x^5*y^7 +9*x^4*y^1 
Second Polynomial:
+9*x^3*y^1 +8*x^5*y^1 
Sum of Polynomials:
+1*x^5*y^7 +9*x^4*y^1 +9*x^3*y^1 +8*x^5*y^1 

Explanation of Code

Header Files and Definitions

#include <stdio.h>
#include <stdlib.h>

The above code includes stdio.h for input/output functions like printf and scanf, and stdlib.h for functions related to memory allocation and other utilities.

 Structure Definition

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 components: coeff for the coefficient, expX for the exponent of x, and expY for the exponent of y. This structure allows the program to store, manage, and manipulate polynomial terms efficiently.

Input Polynomial

void inputPolynomial(Term poly[], int n)

The above function inputPolynomial is used to read a polynomial from the user. It loops n times, and for each term, it prompts the user to enter the coefficientexponent of x, and exponent of y, storing these values in the corresponding fields of the poly array. This allows the program to store all terms of the polynomial for further operations.

Add Two Polynomials

int addPolynomials(Term p1[], int n1, Term p2[], int n2, Term result[])

The above function addPolynomials is used to add two polynomials stored in the arrays p1 and p2 and store the result in the result array. It first keeps track of which terms in p2 have already been used. Then, for each term in p1, it checks if there is a term in p2 with the same exponents. If a matching term is found, their coefficients are added and stored in the result, and the 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 unused terms from p2 are added to the result. The function finally returns the total number of terms in the resulting polynomial.

 Display Polynomial

void displayPolynomial(Term poly[], int n)

The above function displayPolynomial is used to print a polynomial stored in the poly array. It loops through each term and prints it in the format +/-coeff*x^expX*y^expY, showing the sign of the coefficient explicitly. After printing all terms, it outputs a newline to complete the display of the polynomial.

Time & Space Complexity

Time Complexity

OperationTime Complexity
inputPolynomial()O(n)
addPolynomials()O(n1 * n2)
displayPolynomial()O(n)
main() (overall)O(n1 * n2)

Space Complexity

OperationSpace Complexity
inputPolynomial()O(n)
addPolynomials()O(n1 + n2)
displayPolynomial()O(1)
main() (overall)O(n1 + n2)