Untitled

 avatar
unknown
plain_text
a year ago
756 B
4
Indexable
#include <stdio.h>


float func(float x) {
    return x * x * x - 5 * x + 1; // Equation x^3 - x + 11 = 0
}

int main() {
    float a, b, c;
    
    printf("Enter initial approximations of the root: \n");
    scanf("%f %f", &a, &b);
    
    if (func(a) * func(b) >= 0) {
        printf("Root is not lying between a and b\n");
    } else {
        for (int itr = 1; itr <= 15; itr++) {
            c = (a + b) / 2.0;
            if (func(c) == 0) {
                break;
            } else {
                if (func(a) * func(c) < 0)
                    b = c;
                else
                    a = c;
            }
            printf("\nApproximation to the root is: %f", c);
        }
    }
    
    return 0;
}
Editor is loading...
Leave a Comment