fork download
  1. /**
  2.  * Square root using Newton Raphson's methods
  3.  * @author PRATEEK
  4.  */
  5. class NewtonRaphsonSquareRoot {
  6.  
  7. public static void squareRoot(float num)
  8. {
  9. float x1 = num,x2=0,temp=num,e=0.001f;
  10. do
  11. {
  12. x1 = temp;
  13. x2 = 0.5f * (x1 + num/x1);
  14. temp=x2;
  15.  
  16. }while(Math.abs(x2-x1)> e);
  17.  
  18. System.out.println(num + " ----> " + x2);
  19. }
  20.  
  21. public static void main(String[] args) {
  22. squareRoot(44.89f);
  23. squareRoot(2f);
  24. squareRoot(64f);
  25. }
  26. }
Success #stdin #stdout 0.06s 380224KB
stdin
Standard input is empty
stdout
44.89 ----> 6.7
2.0 ----> 1.4142135
64.0 ----> 8.0