JustPaste
HomeCategoriesAboutDonateContactTerms of UsePrivacy Policy
JustPaste

Free online notepad — write and share instantly

Navigate

  • Home
  • Timeline
  • Categories

Info

  • About
  • Donate
  • Contact

Legal

  • Terms of Use
  • Privacy Policy

© 2026 JustPaste.app. All rights reserved.

Made with ♥ by JustPaste

Untitled Page | JustPaste.app
5 months ago78 views
#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--) {
        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;
}

⚠️Content was pasted as plain text and auto-formatted as a code block. Use the Code Block button in the editor for proper formatting.

← Back to timeline