Hi,
I was executing the square root estimation program on Sublime Editor, but unfortunately don’t get any output. I attach the code used with this please and it is also available here https://ide.codingblocks.com/s/358110 and would appreciate your guidance.
Thanks
No output in square root program
Hey @joyjitece the approach you are using won’t be an ideal approach as it will give you run time error for this. Do you want me to help you with an approach?
Hi @mr.encoder ,
Thanks for your reply. Yes please, would appreciate if you could help with an approach. Because of the current approach, the program seems to get stuck/or if I am probably doing something wrong by putting the while loop within the for loop which causes the problem?
Thanks
Hey @joyjitece so there are 2 ways to do this problem, one is simple brute force and the other is with binary search. I’ll explain you binary search approach.
Approach :
1) As the square root of number lies in range 0 <= squareRoot <= number, therefore, initialize start and end as : start = 0, end = number.
2) Compare the square of mid integer with the given number. If it is equal to the number, then we found our integral part, else look for the same in left or right side depending upon the scenario.
3) Once we are done with finding the integral part, start computing the fractional part.
4) Initialize the increment variable by 0.1 and iteratively compute the fractional part upto p places. For each iteration, increment changes to 1/10th of it’s previous value.
5) Finally return the answer computed.
If you will understand the binary approach, you can easily do it with a normal approach.
If you want, I can also help you with the code, but first try it yourself.
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.