Java Calculate Square Root Without Using Library Method

If we want to calculate square root, we can use Math.sqrt() method. It is simple. Now do you know how to write such a method by yourself?

Here is the equation you need.

java square root

The first sqrt number should be the input number / 2.

Using the equation, we can come up with a Java Square Root method by ourselves.

public static double sqrt(int number) {
	double t;
	double squareRoot = number / 2;
	do {
		t = squareRoot;
		squareRoot = (t + (number / t)) / 2;
	} while ((t - squareRoot) != 0);
	return squareRoot;
Category >> CS Courses  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
String foo = "bar";
  • Tsoodol Tserendorj

    U can check number begin lines of function. It’s doesn’t matter.

  • cc

    When the number is o or 1, this method does not work

  • Bartolomeo Barto Club

    Omg misaki u made our entire class laugh haha

  • Misaki

    Is this the babylonian method of square roots?