you can put some print statement and count them. Or use some static counter and increment it in the function. So, any time method is called
one value is incremented and then you can print the value as a last statement in main.
You have written that loop wrongly; you can remove about ⅝ of it.Why are you asking for doubles in your square root method and taking ints from the keyboard? I would suggest you change the loop to do…while (num >= 0); Don't start method names with CapitalLetters.
Campbell Ritchie wrote:You have written that loop wrongly; you can remove about ⅝ of it.Why are you asking for doubles in your square root method and taking ints from the keyboard? I would suggest you change the loop to do…while (num >= 0); Don't start method names with CapitalLetters.
I know i can use Int but the problem is i'm using approximation of the square root that why im using double. by they way if i were to use Int then i won't need this calculation
what i trying to do is like a loop of calculation that output each calculation that the program had done instead of just give me the result or answer without showing the calculation. I think the calculation would look like a right triangle
Don't know. It is such a long time since I saw that algorithm that I have forgotten how to use it. Is that Newton's Approximation? It is at least 45 years since I last saw it. Read about it on Wikipedia or here or here or here or lots of other links
I meant, if your square root method takes a double, why are you getting an int from the keyboard? Why not use nextDouble?
Les Morgan wrote:You're approximation algorithm is flawed:
(N*N)/(2N) -> infinity. Your method is divergent.
you mean i need to do like (high * low )/2 ?
You need to have an algo that converges to your approximation, the formula you have does not.
I did a quick and dirty little algo and this worked for me if I don't be too crazy with it--just put the target number in for the guess and it brings thing around to a good approximation:
dTarget is the number you want the squareroot
dTrial is the guess (put dTarget in here for a start)
dTolerance is the error you are willing to accept (0.0001 seems to work well)
i see thank you for the correction, but somehow the calculation isnt right and return isnt giving my the result if i want sqrt of 25 just give me 1.0 same if i wants to use a number that isnt in the square root like 12 still giving the result of 1.0, i guess this code is wrong, i guess i will go back to the old code