This is a C Program to evaluate the given polynomial equation.

This C Program evaluates the given polynomial equation.

The polynomial equation formula is P(x)=AnXn + An-1Xn-1 + An-2Xn-2+… +A1X + A0.

Here is source code of the C program to evaluate the given polynomial equation. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/* * C program to evaluate a given polynomial by reading its coefficients * in an array. * P(x) = AnXn + An-1Xn-1 + An-2Xn-2+... +A1X + A0 * * The polynomial can be written as: * P(x) = A0 + X(A1 + X(A2 + X(A3 + X(Q4 + X(...X(An-1 + XAn)))) * and evaluated starting from the inner loop */ #include <stdio.h> #include <stdlib.h> #define MAXSIZE 10 void main() { int array[MAXSIZE]; int i, num, power; float x, polySum; printf("Enter the order of the polynomial \n"); scanf("%d", &num); printf("Enter the value of x \n"); scanf("%f", &x); /* Read the coefficients into an array */ printf("Enter %d coefficients \n", num + 1); for (i = 0; i <= num; i++) { scanf("%d", &array[i]); } polySum = array[0]; for (i = 1; i <= num; i++) { polySum = polySum * x + array[i]; } power = num; printf("Given polynomial is: \n"); for (i = 0; i <= num; i++) { if (power < 0) { break; } /* printing proper polynomial function */ if (array[i] > 0) printf(" + "); else if (array[i] < 0) printf(" - "); else printf(" "); printf("%dx^%d ", abs(array[i]), power--); } printf("\n Sum of the polynomial = %6.2f \n", polySum); }

In this C program, we are reading the order of an array using ‘num’ variable and also the value of ‘x’ variable to multiply along with the coefficients. Enter the coefficients of the polynomial equation into an array using for loop.

Initially assign the value of ‘array[0]’ variable to ‘polysum’ variable. Using for loop multiply the value of ‘polysum’ variable with the value of ‘x’ variable and assign the coefficient values of array[i] variable to the ‘polysum’ variable.

After evaluating the output, using If-else condition statement display the coefficients in proper polynomial function. If condition statement is used to check the value of coefficient variables is greater than 0. If the condition is true, then it will display a polynomial equation using ‘+’.

Otherwise, if the condition is false then it will execute the elseif condition statement and display the polynomial equation using ‘-‘. Again if the condition statement is false, then it will execute the else statement and display the absolute value of the array.

$ cc pgm.c $ a.out Enter the order of the polynomial 2 Enter the value of x 2 Enter 3 coefficients 3 2 6 Given polynomial is: + 3x^2 + 2x^1 + 6x^0 Sum of the polynomial = 22.00 $ a.out Enter the order of the polynomial 4 Enter the value of x 1 Enter 5 coefficients 3 -5 6 8 -9 Given polynomial is: + 3x^4 - 5x^3 + 6x^2 + 8x^1 - 9x^0 Sum of the polynomial = 3.00

**Sanfoundry Global Education & Learning Series – 1000 C Programs.**

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

**If you find any mistake above, kindly email to [email protected]**

**Related Posts:**

- Practice Computer Science MCQs
- Watch Advanced C Programming Videos
- Apply for C Internship
- Check Computer Science Books
- Check C Books