Skip to main content
JustPaste
HomeAboutDonateContactTerms of UsePrivacy Policy

© 2026 Just Paste. All rights reserved.

Made with ❤️ by TakiDev

Untitled Page | JustPaste.app
Publish about 1 month ago54 views

//Horner's evaluaation

#include <iostream>

#include <cmath>

using namespace std;

#define EPS 0.00001

void horner(int n, float a[], float x0, float p[]) {

int i;

p[0] = a[n];

p[1] = a[n];

for (i = n - 1; i >= 1; i--) {c

p[0] = a[i] + p[0] * x0;

p[1] = p[0] + p[1] * x0;

}

p[0] = a[0] + p[0] * x0;

}

int main() {

float a[10], p[2], x0, x1;

int i, n, count = 1;

cout << "Enter the degree of the polynomial:\n";

cin >> n;

cout << "Enter the coefficients of the polynomial starting from the highest degree:\n";

for (i = n; i >= 0; i--) {

cin >> a[i];

}

cout << "Enter a valid initial point x0:\n";

cin >> x0;

horner(n, a, x0, p);

x1 = x0 - (p[0] / p[1]);

while (fabs((x1 - x0) / x1) > EPS) {

count++;

x0 = x1;

horner(n, a, x0, p);

x1 = x0 - (p[0] / p[1]);

}

cout << "The approximate root of given function is "

<< x1 << " with " << count<< " number of iterations." << endl;

return 0;

}