Java Code: The following code shows how to get the square root without using the function in Java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | package javaexamples; import java.util.Scanner; public class JavaExamples { public static void main(String[] args) { Scanner reader = new Scanner(System.in); System.out.print("Enter Number: "); double number = reader.nextDouble(); double result; double squareRoot = number / 2; do { result = squareRoot; squareRoot = (result + (number / result)) / 2; } while ((result - squareRoot) != 0); System.out.println("Reult:"+squareRoot); } } |
Output: