#include <stdio.h>

double func(double x)
{ return x*x-2.0;}

double bisection(double xl,double xu)
{
//bisection root finding method
double test;
double xr=0;
double es,ea;
double fxl,fxr;
int maxit=100,iter=0;
es=0.0000001;
ea=1.1*es;
while((ea>es)&&(iter<maxit))
{
    xr=(xl+xu)/2.0;
    iter++;
    if((xl+xu)!=0)
        { ea=Math.abs((xu-xl)/(xu+xl))*100;}
    fxl= func(xl);
    fxr= func(xr);
    test= fxl*fxr;
    if(test==0.0)      ea=0;
    else if(test<0.0)  xu=xr;
    else              {xl=xr;}
} 
if(iter>=maxit) printf("Maximum number of iteration is exceeded \n"+
   " result might not be valid"); 
return xr;
}

int main()
{
   double r=bisection(0.0,2.0);
   return 0;
}
