#include <iostream>
using namespace std;
// Returns the value of the polynomial using Horner's method
int horner(int poly[], int n, int x)
{
int result = poly[0]; // Initialize result
// Evaluate value of polynomial using Horner's method
for (int i = 1; i < n; i++)
result = result * x + poly[i];
return result;
}
int main()
{
int x;
cout << "Enter the value of x: ";
cin >> x;
int n;
cout << "Enter the degree of the polynomial: ";
cin >> n;
n++; // Increment degree to include the constant term
int poly[n];
cout << "Enter the coefficients from cn to c0: ";
for (int i = 0; i < n; i++)
{
cin >> poly[i];
}
// Evaluate the polynomial and print the result
cout << "Value of the polynomial is " << horner(poly, n, x) << endl;
return 0;
}
Comments
Post a Comment