Advertisement
brilliant_moves

SquareRoot.java

Apr 25th, 2014
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.74 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class SquareRoot {
  4.  
  5.     /**
  6.     *   Program:    SquareRoot.java
  7.     *   Purpose:    Compute square root without using sqrt function
  8.     *   Creator:    Chris Clarke
  9.     *   Created:    29.07.2013
  10.     */
  11.  
  12.     public static void main(String[] args) {
  13.  
  14.         Scanner scan = new Scanner(System.in);
  15.         System.out.print("Enter a number: ");
  16.  
  17.         String strNum = scan.nextLine();
  18.         double theNumber = Double.parseDouble(strNum);
  19.  
  20.         System.out.println("The square root of "+theNumber+" = "
  21.          +findSquareRoot(theNumber));
  22.     }//end main
  23.  
  24.     public static double findSquareRoot(double theNumber) {
  25.  
  26.         double memory=2;
  27.  
  28.         for (int i=0; i<10; i++) {
  29.             memory = ((theNumber/memory)+memory)/2;
  30.         }
  31.  
  32.         return memory;
  33.  
  34.     }//end findSquareRoot
  35.  
  36. }//end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement