#include <iostream>
#include <math.h>

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

double dfunc(double x)
{ return 2.0*x;}

double newton(double x)
{
int nmax=100;
double tolerance=1.0e-8;
double fx,dfx;
for(int i=0;i<nmax;i++)
{
  fx=func(x);
  dfx=dfunc(x);
  x-=fx/dfx;
  std::cout <<"i="<<i<<"x="<<x<<"fx="+fx+"dfx="+dfx+"\n";
  if(fabs(fx)<tolerance) { return x;}
}
std::cout <<("Maximum number of iteration is exceeded \n result might not be valid");
return x;
}

int main()
{  double xa,r;
   std::cout << " first guess for the root value : ";
   std::cin >>xa;

   r=newton(xa);
   std::cout<<"root value : ="<<r<<"fr="<<func(r)-r;
   return 0;
}
