A polynomial of degree N is an expression of the form:
P(x) = ANxN + AN-1xN-1 + … + A1x + A0
The naïve way to evaluate this requires computing each power xk separately, which takes O(N²) multiplications in total. Horner’s method rewrites the polynomial as a nested product that requires only N multiplications and N additions — O(N) total — with no pow() calls:
P(x) = ((AN·x + AN-1)·x + AN-2)·x + … + A0
The original post used conio.h, voidmain() (literally — no space), clrscr(), and float. This rewrite fixes all of that and explains the algorithm clearly.
Horner’s Method — Step-by-Step for P(x) = x² − 3x + 2, x=2
| Step | Operation | Result |
|---|---|---|
| Start | result = a[0] = 1 | 1 |
| i=1 | result = 1 × 2 + (−3) = 2 − 3 | −1 |
| i=2 | result = (−1) × 2 + 2 = −2 + 2 | 0 |
P(2) = 2² − 3(2) + 2 = 4 − 6 + 2 = 0 ✓ — 2 is a root.
C Program: Polynomial Evaluation Using Horner’s Method
/* Evaluate polynomial P(x) = a[0]*x^N + a[1]*x^(N-1) + ... + a[N]
* using Horner's method: O(N) multiplications, no pow() calls
* Compile: gcc -ansi -Wall -Wextra polyeval.c -o polyeval */
#include <stdio.h>
#include <stdlib.h> /* abs() */
#define MAXDEG 20
int main(void)
{
int a[MAXDEG + 1];
int i, N;
double x, result;
printf("Enter degree of polynomial (0-%d): ", MAXDEG);
if (scanf("%d", &N) != 1 || N < 0 || N > MAXDEG) {
printf("Enter a degree between 0 and %d.\n", MAXDEG);
return 1;
}
printf("Enter value of x: ");
if (scanf("%lf", &x) != 1) { printf("Invalid.\n"); return 1; }
printf("Enter %d coefficient(s), highest degree first:\n", N + 1);
for (i = 0; i <= N; i++) {
if (scanf("%d", &a[i]) != 1) { printf("Invalid.\n"); return 1; }
}
/* Horner's method: accumulate from highest to lowest coefficient */
result = a[0];
for (i = 1; i <= N; i++)
result = result * x + a[i];
/* Print the polynomial */
printf("P(x) = ");
for (i = 0; i <= N; i++) {
int deg = N - i;
if (a[i] == 0) continue;
if (i > 0 && a[i] > 0) printf(" + ");
else if (a[i] < 0) printf(" - ");
if (deg == 0)
printf("%d", abs(a[i]));
else if (deg == 1)
printf("%dx", abs(a[i]));
else
printf("%dx^%d", abs(a[i]), deg);
}
printf("\n");
printf("P(%.2f) = %.4f\n", x, result);
return 0;
}
How to Compile and Run
gcc -ansi -Wall -Wextra polyeval.c -o polyeval
./polyeval
Sample Output
Enter degree of polynomial (0-20): 2 Enter value of x: 2.0 Enter 3 coefficient(s), highest degree first: 1 -3 2 P(x) = 1x^2 - 3x + 2 P(2.00) = 0.0000
Enter degree of polynomial (0-20): 3 Enter value of x: 1.5 Enter 4 coefficient(s), highest degree first: 2 0 -1 3 P(x) = 2x^3 - 1x + 3 P(1.50) = 10.2500
Naïve vs Horner — Operation Count
| Method | Multiplications | Additions | pow() calls |
|---|---|---|---|
| Naïve (compute each x^k) | O(N²) | O(N) | N |
| Horner’s method | O(N) | O(N) | 0 |
Code Explanation
- Coefficients entered highest-degree first — for P(x) = x² − 3x + 2, you enter 1 (for x²), −3 (for x), 2 (constant). Array index 0 holds the coefficient of the highest power; index N holds the constant. This matches the way polynomials are written mathematically.
- Horner’s loop:
result = result * x + a[i]— each iteration multiplies the accumulated result by x and adds the next lower-degree coefficient. After N iterations, result equals P(x). No power of x is ever computed explicitly. doublenotfloat— the original usedfloat x, polySum. For polynomial evaluation where intermediate results multiply repeatedly, rounding errors accumulate.double(64-bit, ~15 significant digits) avoids the large errors thatfloat(32-bit, ~7 significant digits) produces for high-degree polynomials.- Polynomial display loop — terms with zero coefficients are skipped. The sign is tracked separately so positive terms print ” + ” before them (after the first term) and negative terms print ” – ” with the absolute value, matching standard mathematical notation.
What This Program Teaches
- Horner’s method — a fundamental algorithm — this is not just a textbook trick. It is used in every production floating-point library for evaluating polynomial approximations of transcendental functions (sin, cos, exp, log). Understanding it gives you a window into how FPUs work.
- Algorithmic complexity matters — the difference between O(N²) and O(N) is tiny for N=5, but for N=100 it is 100× fewer multiplications. Floating-point multiplication is not free — on embedded hardware, it can cost dozens of clock cycles.
- Reformulating problems — the key insight of Horner’s method is factoring out x repeatedly. This is a general technique: look for opportunities to rewrite expressions to eliminate repeated computation. The same idea drives Strassen matrix multiplication and the FFT.
Related Programs
- sin(x) Taylor Series in C
- Quadratic Equation Roots in C
- Compute x^n (Binary Exponentiation) in C
- GCD and LCM in C
- Mean, Variance, Standard Deviation in C
Recommended book:
The C Programming Language — Kernighan & Ritchie (India) |
(US)
|
C Programming: A Modern Approach — K.N. King (India) |
(US)
Practice what you learned: C Aptitude Questions — or try our C Programming Quiz App on Android.