#include<iostream>
#include<cmath>
using namespace std;
int main (){
float t,x,xnew,xold,temp,diff;
int count=0;
cout<<"Enter the number"<<endl;
cin >> x;
cout<< "Enter the guess value"<<endl;
cin>>xold;
cout<<"Enter the tolerance"<<endl;
cin>>t;
do{
temp=xold;
xnew=0.5*(xold + x/xold);
xold=xnew;
count=count+1;
diff=abs(xnewtemp);
}while(diff>t);
cout<< xnew<<endl;
cout<<count;
return 0;
}
Changing the number of decimal places displayed by the cout function 
+3 
acevan s
· December 19, 2014
Hi guys. Just needed a little help understanding something. I wrote a little program that should calculate a root of a number iteratively and should stop when the xnew and xold values differ by less than the tolerance value. However irrespective of the tolerance value the output always has a fixed resolution of 5 decimal places. I dont understand why. Maybe someone can point out my mistake. Thanks.

Post a Reply 
Replies 
Oldest Newest Rating 
0 
acevan s
· December 25, 2014
Thanks a lot Stan. That did the trick

+1 
Not Shifty
· December 21, 2014
If you look at the limits, it will tell you that floats can take up to 38 zeros(?) and doubles can go up to 308 zeros (?). 10 zeros is barely stretching the boundaries....

0 
acevan s
· December 21, 2014
Also i tried the fixed output thing in a integer factorial program. It gave a really big space for the digits before the decimal but still showed 6 decimal places with zeroes. eg 89856216184984651654986523.000000

0 
acevan s
· December 21, 2014
Checked that option already. No difference at all.

0 
acevan s
· December 19, 2014
Thanks for your reply. I tried both of your methods(copy and paste (: because i am just a beginner) but they give answers only upto 6 decimal places. I want an answer like you see it on a calcuators display.. you know. I am attaching a pic of the output with your solution. Unfortunately there is some bug which is preventing me from uploading a pic. so i will just write it down :/
Enter the number 15 Enter the guess value 5 Enter the tolerance 0.00000000001 3.872983 5 Process returned 0 
 1