How to Find Value of Polynomial Using Horner's Rule in C++
- Find the Value of Polynomial Using Horner’s Rule in C++ by Iterating the Loop Backward
- Find the Value of Polynomial Using Horner’s Rule in C++ by Iterating the Loop Forward
- Use Recursion to Find the Value of Polynomial Using Horner’s Rule in C++
- Conclusion
 
Horner’s rule is an efficient algorithm for computing the value of a polynomial.
Consider the polynomial p(x) = 6x^3 - 2x^2 + 7x + 5 at x = 4. To compute it using Horner’s rule in C++, the first coefficient, 6, is multiplied by the value at x, which is 4, and the product of the two being 24, gets added to the next coefficient -2.
The resultant is multiplied by 4 and added to the next coefficient.
This process is repeated for the number of coefficients in the equation. The end product left is the value of the polynomial.
This article explains how to find the value of a polynomial using Horner’s rule in C++ by converting the above rules into computer code.
Let’s consider a polynomial:
If x = 4, the value of the polynomial is 385.
Find the Value of Polynomial Using Horner’s Rule in C++ by Iterating the Loop Backward
Let’s look at the program that finds the value of a polynomial using Horner’s rule in C++ by backward looping.
- 
The first line of code imports the library package iostream; in the second line, the namespace is defined asstd.
- 
A method int Horner()is created with three parameters - a pointer arrayathat will pass the list of coefficients, integer variablenwhich stores the degree of the polynomial, and another integer variablexwhich stores the polynomial’s value atx.int Horner(int* a, int n, int x)
- 
This explains how to add the product of polynomials to each other. An integer variable resultis created, initialized with the last element of the arraya[n].
```c++
int result = a[n];
```
> Note: Do not mistake it as the size of the array; it initializes the variable `result` with the element in array `a` at the nth position.
- 
A forloop is created, reversing the loop from the last element of arrayato the 0th index. The value of theresultvariable gets updated for each loop iteration.for (int i = n - 1; i >= 0; --i)In the first iteration, the value of a[n]is multiplied withxand then added to the previous element in the array -a[n-1]. For an array -{2,3,4}, the loop will take the last elementa[n], which is 4, multiply it withx, and then add to then-1element of the array, which is 3.For this reason, the coefficients need to be reversed while initializing in the mainfunction. The value ofresultis returned at the end of the method.
- 
Inside the mainfunction, an array is created to pass it to the first parameter of methodHorner. Note that the value of coefficients inside the array is reversed because the loop iterates backward.
- 
An integer variable solis created to store the result returned from the method namedHorner.In the first parameter, the array that is being created is passed. The second parameter passes the degree of polynomials in the equation, 3, and the third parameter value at xis passed.int sol = Horner(arr, 3, 4);
Code:
#include <iostream>
using namespace std;
int Horner(int* a, int n, int x) {
  int result = a[n];
  for (int i = n - 1; i >= 0; --i) result = result * x + a[i];
  return result;
}
int main() {
  int arr[4] = {5, 7, -2, 6};
  int sol = Horner(arr, 3, 4);
  cout << sol;
}
Output (Backward looping Horner’s rule in C++):
385
Find the Value of Polynomial Using Horner’s Rule in C++ by Iterating the Loop Forward
If we need to run the loop forward, unlike the last example, we can dereference the pointer of the array to get its first element and run the loop forward instead of reversing it. It is another way to use loops to implement Horner’s rule in C++.
Let’s understand what the code does:
- The first line of code imports the library package iostream.
- Similar to the previous example, a method Horneris created with three parameters.
```c++
int Horner(int a[], int n, int x)
```
- 
A variable resultis created and initialized with the array’s first element by dereferencing the pointer. The pointer*ais the same as writingarr[0].int result = *a;In the previous example, the method Hornerneeded a size component attached to its array:int result = a[n];to point to the last element. Here, it is just dereferenced.
- 
A forloop is created that iterates from 1 ton. In each iteration, the value of theresultvariable is updated with the value of the polynomial.The value of resultis returned at the end of the method.for (int i = 1; i < n; i++) result = result * x + a[i]; return result;
- 
Inside the mainfunction, an array is created with the value of coefficients. The polynomial used here is:$$ 
 p(x) = 5x^4 + 3x^3 - 2x^2 + 4x - 6; x=-2
 $$The array is created by taking the coefficients of the polynomials - int arr[] = {5,3,-2,4,-6};.
- 
The variable nholds the degree of the polynomial. The value ofnis computed by:n = size of array -1.int n = (*(&arr + 1) - arr) - 1; // n = size of the array - 1;
- 
The method Horneris called by passing the array, value ofn, and value atx. The returned result is stored in a new integer variable and printed.int sol = Horner(arr, n, -2); std::cout << "Value of polynomial = " << sol;
Code:
#include <iostream>
int Horner(int a[], int n, int x) {
  int result = *a;
  for (int i = 1; i < n; i++) result = result * x + a[i];
  return result;
}
int main() {
  int arr[] = {5, 3, -2, 4, -6};
  int n = (*(&arr + 1) - arr) - 1;
  int sol = Horner(arr, n, -2);
  std::cout << "Value of polynomial = " << sol;
}
Output (Forward looping Horner’s rule in C++):
Value of polynomial = -20
Use Recursion to Find the Value of Polynomial Using Horner’s Rule in C++
So far, we have learned how to find the value of a polynomial using Horner’s rule in C++. It was done by iterating loops and updating the count using an accumulator variable.
In this example, the value of the polynomial is computed by recursion. Let’s look at the code.
- 
The first line of code imports package iostreamand defines namespace asstd.#include <iostream> using namespace std;
- 
A method Horneris created which has three parameters - a pointer integer*pi, another integer variabledegree, which is the degree of the polynomial (used asnin the previous example), andxfor passing value atx.int Horner(int* pi, int degree, int x)
- 
The feature of the recursive function is to keep calling itself like a loop until a condition is met, which reduces the time complexity. For the condition, an ifstatement returns the value ofpiat the 0th index only if the degree’s value has reduced to 0.
```c++
if (degree == 0) {
  return pi[0];
}
```
- 
To apply recursion, the method Horneris called again instead of returning a value.return Horner(pi, degree - 1, x) * x + pi[degree];The above syntax keeps the Hornermethod to call itself repeatedly for the degree of the polynomial present. In an equation, the degree of the polynomial is its highest exponent.If the equation used in the last example is considered: $$ p(x) = 5x^4 + 3x^3 - 2x^2 + 4x - 6; x=-2 $$The degree of the polynomial is 4. This means the recursion will iterate itself for n+1= 5 times (size of the array). At each iteration, the method will call itself until the degree’s value is reduced to0.Once it reaches 0, the recursion will take the*p[0]element and pass it to its previous method call.
```c++
return Horner(pi, degree - 1, x) * x + pi[degree];
```
It is the same as opening an envelope inside an envelope until the last one is reached and then folded back again. The value at the last method call gets passed to its previous method call, and the computations are executed.
- 
Inside the mainfunction, an integer array is created to pass the coefficients of the equation to the methodHorner. Then the value of the parameters is passed to the method.int sol = Horner(arr, 4, -2);The array is arr, degree -4, and value atx, which is-2.
- 
Lastly, the returned result is stored in an integer variable soland gets printed.
Code:
#include <iostream>
using namespace std;
int Horner(int* pi, int degree, int x) {
  if (degree == 0) {
    return pi[0];
  }
  return Horner(pi, degree - 1, x) * x + pi[degree];
}
int main() {
  int arr[] = {5, 3, -2, 4, -6};
  int sol = Horner(arr, 4, -2);
  cout << "Value of Polynomial using recursion = " << sol;
}
Output:
Value of Polynomial using recursion = 34
Conclusion
This article explains how to find the value of polynomials using Horner’s rule in C++. The three methods have varying time complexities which can be a great learning tool for the reader.
It is recommended to try writing the codes yourself and then coming back to get hints. The reader will easily understand the concepts of finding the value of polynomials using Horner’s rule in C++.