The desired accuracy is upto 6 decimal places. The formula for finding a real root of an equation f(x) = 0 is given by
Xn+1= Xn– f(xn)/f ‘(xn) n = 0, 1, 2…
X0 denotes the initial approximate value of the root. This value is modified at each iteration. The absolute value of the difference between the new and the old value of the root is computed at the end of each iteration. When the difference reaches an acceptable level of accuracy, control goes out of the loop.
#include <iostream.h> #include <math.h> #include<conio.h> void main() { float x0,xl,x,error=2; int i; clrscr(); cout<<"Enter the initial value of root \n"; cin>>x0; cout<<"error "<<error; while (error>0.1e-5) { xl =x0-(x0*x0+2*x0+1)/(2*x0+2); error = fabs (xl-x0); cout<<"Error "<<error; x0=xl; } cout<<"The real root is "<<x0; getch(); }