# 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 down this process into easy-to-understand steps.

## Understanding Single-Variable Polynomials

A single-variable polynomial is an algebraic expression composed of terms. Each term has a coefficient (a number) and a variable (such as x) raised to an exponent, which is a non-negative integer. For instance, in the polynomial 5x3+2x2−x+4, each term includes a coefficient and a variable raised to an exponent.

### Steps in Adding Single-Variable Polynomials

To add single-variable polynomials, follow these steps:

1. Identify Like Terms: First, identify terms in the polynomials that have the same exponent. These are known as ‘like terms’. For example, x2 terms in different polynomials are like terms.
2. Combine Coefficients: Add together the coefficients of these like terms. The variable part of the term remains unchanged. For instance, if one polynomial has 3x2 and another has 5x2, their sum would be (3+5)x2=8x2.
3. Construct the Resulting Polynomial: Write down the sum of the like terms to form the resulting polynomial. Typically, terms are arranged in descending order of their exponent values.
4. Simplify the Polynomial: If there are any like terms in the new polynomial, combine them to further simplify the expression.

### Example

Let’s demonstrate this with an example. Consider adding these two polynomials:

• P(x)=4x3+3x2−x+6
• Q(x)=2x3−5x2+2x+4

Here’s how you add them:

• Step 1: Identify like terms (same exponent).
• Step 2: Add the coefficients of like terms.
• 3(4x3+2x3)=6x3
• 2(3x2−5x2)=−2x2
• (−x+2x)=x
• 6+4=10
• Step 3: Combine these to form the resulting polynomial: 6x3−2x2+x+10.

## Algorithm for Addition of Single Variable Polynomial

The algorithm involves representing polynomials as arrays, where each index corresponds to the exponent, and the value at that index is the coefficient of the corresponding term.

1. Initialize Polynomials: Represent each polynomial by an array, with the array index representing the exponent and the value at that index representing the coefficient.
2. Determine Maximum Size: Find the size of the larger polynomial to set the size of the result array.
3. Create Result Array: Initialize an array of the determined size to store the sum of the polynomials.
4. Add Polynomials: Iterate over the arrays representing the polynomials. Add the coefficients of like terms (same exponent) and store them in the corresponding index of the result array.
5. Handle Remaining Terms: If one polynomial is larger, copy the remaining terms to the result array.
6. Display Result: Output the resulting polynomial.

## C Program to Add Two Polynomial

Here is a C program that demonstrates the addition of single-variable polynomials:

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

#define MAX_DEGREE 100  // Maximum degree for the polynomial

void polynomialAddition(int polynomial1[], int polynomial2[], int degree1, int degree2) {
int maxDegree = degree1 > degree2 ? degree1 : degree2;
int result[MAX_DEGREE] = {0};

for (int i = 0; i <= maxDegree; i++) {
if (i <= degree1) result[i] += polynomial1[i];
if (i <= degree2) result[i] += polynomial2[i];
}

printf("Resultant polynomial = ");
for (int i = maxDegree; i >= 0; i--) {
if (result[i] != 0) {
printf("%dx^%d ", result[i], i);
if (i != 0) printf("+ ");
}
}
printf("\n");
}

int main() {
// Polynomial 1: 6x^3 + 3x^2 + 9x^1 + 1
int polynomial1[] = {1, 9, 3, 6}; // degree 3
// Polynomial 2: 5x^2 + 3x^1 + 2
int polynomial2[] = {2, 3, 5}; // degree 2

polynomialAddition(polynomial1, polynomial2, 3, 2);
return 0;
}
``````

Output

`Resultant polynomial = 6x^3 + 8x^2 + 12x^1 + 3x^0`

### Explanation

This C program demonstrates the addition of two single-variable polynomials using arrays. Here’s a step-by-step explanation:

#### Program Structure

1. Preprocessor Directives: `#include <stdio.h>` and `#include <stdlib.h>` are used for standard input-output and standard library functions.
2. Macro Definition: `#define MAX_DEGREE 100` sets a maximum degree for the polynomial, defining the size of the arrays that will represent the polynomials.

``````// Polynomials Addition
void polynomialAddition(int polynomial1[], int polynomial2[], int degree1, int degree2) {
int maxDegree = degree1 > degree2 ? degree1 : degree2;
int result[MAX_DEGREE] = {0};

for (int i = 0; i <= maxDegree; i++) {
if (i <= degree1) result[i] += polynomial1[i];
if (i <= degree2) result[i] += polynomial2[i];
}

printf("Resultant polynomial = ");
for (int i = maxDegree; i >= 0; i--) {
if (result[i] != 0) {
printf("%dx^%d ", result[i], i);
if (i != 0) printf("+ ");
}
}
printf("\n");
}``````
• Purpose: To add two polynomials.
• Parameters:
• `polynomial1[]`, `polynomial2[]`: Arrays representing the coefficients of two polynomials.
• `degree1`, `degree2`: Degrees of these polynomials.
• Local Variables:
• `maxDegree`: Holds the higher degree of the two polynomials.
• `result[]`: An array to store the sum of the polynomials.
• Process:
• It calculates the maximum degree of the two polynomials.
• Initializes the `result` array to zero.
• Iterates through each term (up to `maxDegree`), adding the coefficients of like terms (same degree exponent) from both polynomials.
• Output:
• The function prints the resultant polynomial after addition. Terms are displayed in descending order of degree.

#### Function: main

``````int main() {
// Polynomial 1: 6x^3 + 3x^2 + 9x^1 + 1
int polynomial1[] = {1, 9, 3, 6}; // degree 3
// Polynomial 2: 5x^2 + 3x^1 + 2
int polynomial2[] = {2, 3, 5}; // degree 2

polynomialAddition(polynomial1, polynomial2, 3, 2);
return 0;
}
``````
• Purpose: Entry point of the program.
• Process:
• Two polynomials are defined through arrays `polynomial1` and `polynomial2` with their respective coefficients. The index of the array represents the exponent, and the value at that index represents the coefficient.
• For example, `polynomial1[] = {1, 9, 3, 6}` represents the polynomial `6x^3 + 3x^2 + 9x + 1`.
• Function Call:
• `polynomialAddition(polynomial1, polynomial2, 3, 2)` is called to add these polynomials.
• Output:
• The main function calls `polynomialAddition` which then prints the result.

Hope this tutorial helped you to understand how to add two single variable polynomials.