Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class SquareRoot {
- /**
- * Program: SquareRoot.java
- * Purpose: Compute square root without using sqrt function
- * Creator: Chris Clarke
- * Created: 29.07.2013
- */
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- System.out.print("Enter a number: ");
- String strNum = scan.nextLine();
- double theNumber = Double.parseDouble(strNum);
- System.out.println("The square root of "+theNumber+" = "
- +findSquareRoot(theNumber));
- }//end main
- public static double findSquareRoot(double theNumber) {
- double memory=2;
- for (int i=0; i<10; i++) {
- memory = ((theNumber/memory)+memory)/2;
- }
- return memory;
- }//end findSquareRoot
- }//end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement