Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.0 kB
2
Indexable
Never
#include <stdio.h>
#include <math.h>
float f(float x)
{
    return (x * exp(x) - 3);
}
void main()
{
    float a, b, x, aerr;
    int itr = 0, flag = 0;
    do
    {
        printf("Enter the two values for interval : ");
        scanf("%f%f", &a, &b);
        if (f(a) * f(b) < 0)
        {
            printf("\nThe root of the equation lies between %.2f and  %.2f\n", a, b);
            flag = 1;
        }
        else
            printf("\nWrong Interval!!\n");
    } while (flag == 0);
    printf("\nEnter the allowed error");
    scanf("%f", &aerr);
    do
    {

        itr++;
        x = (a+b)/2;
        printf("\n%d Iteration, value of X[%d] is = %f, value of f(x)=%f",itr,itr,x,f(x));
        if (fabs(f(x)) <= aerr)
        {
            printf("\nThe root of the equation is %f after %d iteration",x,itr);
            flag = 0;
        }
        else if (f(a) * f(x) < 0)
            b = x;
        else
            a = x;
    }
    while (flag==1);
    {
    }
}
    
Leave a Comment