 # C Programming Code Examples

## C > Mathematics Code Examples

### Program to Evaluate the given Polynomial Equation

``` 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. /* - 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 j, 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 (j = 0; j <= num; j++) { scanf("%d", &array[j]); } polySum = array; for (j = 1; j <= num; j++) { polySum = polySum * x + array[j]; } power = num; printf("Given polynomial is: \n"); for (j = 0; j <= num; j++) { if (power < 0) { break; } /* printing proper polynomial function */ if (array[j] > 0) printf(" + "); else if (array[j] < 0) printf(" - "); else printf(" "); printf("%dx^%d ", abs(array[j]), power--); } printf("\n Sum of the polynomial = %6.2f \n", polySum); } ``` 