// Listing 2.2 usingcout
#include <iostream>
   public double func (double x)
   { return x*x-4;}
public 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.00000001;
ea=1.1*es;
String s="";
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);
    s+="xr="+xr+"fxr="+fxr+"iter="+iter+"\n";
    test= fxl*fxr;
    if(test==0.0)      ea=0;
    else if(test<0.0)  xu=xr;
    else              {xl=xr;}
} 
if(iter>=maxit) std::cout<<"Maximum number of iteration is exceeded \nresult might not be valid"; 
   return xr;
}
   
   
int main()
{    using std::cout;  
     using std::cin; 
     using std::endl;
     cout << " INPUT LOWER LİMİT OF SEARCH AREA a : ";
     cin >>a;
     cout << " INPUT UPPER LİMİT OF SEARCH AREA b : ";
     cin >>b;
     double r;
     r= bisection(a,b);
      cout << "x = "<<r;
     return 0;
  }
   