fork(9) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main(String[] args)
  11. {
  12.  
  13. float Number = 123.45f;
  14.  
  15. System.out.println("The square root of " + Number + " is " + Compute(Number));
  16. }
  17.  
  18. public static float Compute(float Number)
  19. {
  20. // define variable sqrRoot to hold the approximate square root
  21. float sqrRoot = 0;
  22. // define temporary variable temp to hold prior value of iteration
  23. float temp = 0;
  24. // divide variable num by 2 to start the iterative process
  25. // and assign the quotient to temp
  26. temp = Number/2;
  27. // open a while() loop that continues as long as num >= 0.0
  28. while (Number >= 0.0)
  29. {
  30. // construct the main iterative statement
  31. sqrRoot = temp - (temp * temp - Number) / (2 * temp);
  32. // open an if block to check if the absolute value of the difference of
  33. // variables temp and sqrRoot is below a small sentinel value such as 0.0001
  34. // if this condition is true then break the loop
  35. float value;
  36. value = Math.abs(temp - sqrRoot);
  37. if (value < .0001)
  38. // return sqrRoot as the answer
  39. return sqrRoot;
  40. // if this condition is not true then assign sqrRoot to temp
  41. else temp = sqrRoot;
  42.  
  43. // close the while() loop
  44. }
  45. return Number;
  46. }
  47. }
  48.  
Success #stdin #stdout 0.08s 380224KB
stdin
Standard input is empty
stdout
The square root of 123.45 is 11.1108055