#include <iostream>
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
 
double f(double x)
{
    return x * x * x - 4; 
}
 
int main()
{
    double x0, x1, x2, f0, f1, f2;
    int maxi;
    double t;
 
    cout << "Enter the value of x0: ";
    cin >> x0;
    cout << "Enter the value of x1: ";
    cin >> x1;
    cout << "Enter maximum iterations: ";
    cin >> maxi;
    cout << "Enter tolerance (e.g., 0.0001): ";
    cin >> t;
 
    f0 = f(x0);
    f1 = f(x1);
 
    if (f0 * f1 > 0) {
        cout << "\nInvalid interval!.\n";
        return 0;
    }
 
    cout << "\n-------------------------------------------------------------";
    cout << "\nIteration\t x0\t\t x1\t\t x2\t\t f0\t\t f1\t\t f2";
    cout << "\n-------------------------------------------------------------\n";
 
    int i = 1;
    while (i <= maxi) {
        x2 = x0 - (f0 * (x1 - x0)) / (f1 - f0);
        f2 = f(x2);
 
        cout << fixed << setprecision(6);
        cout << setw(3) << i << "\t"
             << x0 << "\t" << x1 << "\t" << x2 << "\t"
             << f0 << "\t" << f1 << "\t" << f2 << endl;
 
        if (fabs(f2) < t) { 
            break;
        }
 
        if (f0 * f2 < 0) {
            x1 = x2;
            f1 = f2;
        } else {
            x0 = x2;
            f0 = f2;
        }
 
        i++;
    }
 
    cout << "\nApproximate root = " << x2 << endl;
    return 0;
}