How to implement Bisection Method in C Programming ?

 #include<stdio.h>
    #include<math.h>
    float f(float x)
    {
        return (x*x*x)-(3*x)+1;
    }
    main()
    {
        printf("\nProgram to implement Bisection method : \n");
        float a,b,m;
        int i,c=1;
        printf("\nEnter the lower and upper approximation root : \n");
        scanf("%f%f",&a,&b);
        if(f(b)<f(a))
        {
            m=a;
            a=b;
            b=m;
        }
        if((f(a)>0 && f(b)>0)||(f(a)<0 && f(b)<0))
        {
            printf("\nNo root present...");
            return;
        }
        while((fabs(a-b)>0.001)&& c!=40)
        {
            c++;
            m=(a+b)/2;
            if(f(m)>0)    b=m;
            else        a=m;
        }
        printf("\nRoot = %f\n",a);
    } 

Top