Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
756 B
3
Indexable
Never
#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;
}
Leave a Comment