Saturday, 28 September 2024

Write a Program to implement addition of two polynomials using 2 dimensional array.

 #include <stdio.h>

#include <conio.h>


// Function to add two polynomials

void addPolynomials(int poly1[][2], int poly2[][2], int result[][2], int n) {

    for (int i = 0; i < n; i++) {

        result[i][0] = poly1[i][0] + poly2[i][0];

    }

}


// Function to display a polynomial

void displayPolynomial(int poly[][2], int n) {

    for (int i = 0; i < n; i++) {

        printf("%d", poly[i][0]);

        if (i != 0)

            printf("x^%d", i); // Display power of x

        if (i != n - 1)

            printf(" + "); // Display '+' between terms

    }

    printf("\n");

}


int main() {

    int n; // Degree of the polynomial + 1

    printf("Enter the number of terms in the polynomials: ");

    scanf("%d", &n);


    int poly1[100][2], poly2[100][2], result[100][2]; // Arrays for the two polynomials and the result


    // Input for the first polynomial

    printf("Enter the coefficients of the first polynomial:\n");

    for (int i = 0; i < n; i++) {

        printf("Coefficient of x^%d: ", i);

        scanf("%d", &poly1[i][0]);

    }


    // Input for the second polynomial

    printf("Enter the coefficients of the second polynomial:\n");

    for (int i = 0; i < n; i++) {

        printf("Coefficient of x^%d: ", i);

        scanf("%d", &poly2[i][0]);

    }


    // Add the two polynomials

    addPolynomials(poly1, poly2, result, n);


    // Display the result

    printf("First Polynomial: ");

    displayPolynomial(poly1, n);


    printf("Second Polynomial: ");

    displayPolynomial(poly2, n);


    printf("Resultant Polynomial after addition: ");

    displayPolynomial(result, n);


    getch(); 

    return 0;

}

Method 2

#include <stdio.h>


// Function to add two polynomials

void addPolynomials(int poly1[][2], int poly2[][2], int result[][2], int n)

 {

    for (int i = 0; i < n; i++) {

        result[i][0] = poly1[i][0] + poly2[i][0]; // Add coefficients

        result[i][1] = poly1[i][1];             

  // Exponent remains the same

    }

}


// Function to display a polynomial

void displayPolynomial(int poly[][2], int n) {

    for (int i = 0; i < n; i++) {

        // Display coefficient and exponent

        printf("%dx^%d", poly[i][0], poly[i][1]);

        if (i != n - 1) {

            printf(" + "); // Display '+' between terms

        }

    }

    printf("\n");

}


int main() {

    int n; // Number of terms in the polynomial


    printf("Enter the number of terms in the polynomials: ");

    scanf("%d", &n);


    int poly1[100][2], poly2[100][2], result[100][2]; // Arrays for the two polynomials and the result


    // Input for the first polynomial

    printf("Enter the coefficients and exponents of the first polynomial:\n");

    for (int i = 0; i < n; i++) {

        printf("Coefficient of term %d: ", i + 1);

        scanf("%d", &poly1[i][0]);

 // Coefficient

        printf("Exponent of term %d: ", i + 1);

        scanf("%d", &poly1[i][1]); 

// Exponent

    }


    // Input for the second polynomial

    printf("Enter the coefficients and exponents of the second polynomial:\n");

    for (int i = 0; i < n; i++) {

        printf("Coefficient of term %d: ", i + 1);

        scanf("%d", &poly2[i][0]);

 // Coefficient

        printf("Exponent of term %d: ", i + 1);

        scanf("%d", &poly2[i][1]);

 // Exponent

    }


    // Add the two polynomials

    addPolynomials(poly1, poly2, result, n);


    // Display the result

    printf("\nFirst Polynomial: ");

    displayPolynomial(poly1, n);


    printf("Second Polynomial: ");

    displayPolynomial(poly2, n);


    printf("Resultant Polynomial after addition: ");

    displayPolynomial(result, n);


    return 0;

}