Square Root(Brute force)

result of square root shows up to 5 digits only(after decimal) although I gave it precison of 10.
Eg. square root of 10 with precision 10 gives output 3.16228
also i have change the ans from float to double still not works???

Hi @pm20080,
try using dis,
double n=1.
cout << fixed << setprecision(10) << pi <<" "<<n<<endl;
dis will give decimal values upto 10 decimal places.
use above methods in questions where it is given to print ans upto m decimal places.
Hope dis resolves ur doubt.

1 Like

thank u sir!!! but I have to do it without using predefined function: Here is my code of Brute force algo:-

#include
#include <stdlib.h>
using namespace std;

int main()
{
int n,p;
cin>>n>>p;
double ans=0;
float inc=1.0;

while((p+1)!=0)
{
    while(ans*ans<=n)
    {
        ans+=inc;
    }
    ans=ans-inc;
    inc=inc/10;
    p--;
}

cout<<ans<<endl;
system("pause");
return 0;

}

So when if give input : 10 10
output: 3.16228

@pm20080, without using setprecision fuction or any other function, u can print only upto 6 decimal places(at max). Ur code for calculating ans is write but while outputting,the ans is rounded off. eg. if ans=3.162277,output ur code will give is 3.16228. So,in order to get upto 10 decimal places in ur printed output, use above method or
printf("%.10f",ans);
If u still hv any doubt, pls post them here.

1 Like

so its actually kind of predefined inside C++ to round off to 5 decimal precision and so if we require more precision, we will hv to do it this way.
I got it.

THANK U SO MUCH SIR!!! IT WAS REALLY HELPFULL!!!

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.