Finding Square Root of a Number in Java

In this post we will see an example on finding the Square Root of a Number. Square Root of a given number n is y, such that square of y is n.

Below is the program to print square root of a given number.


package com.techstackjournal;

public class SquareRootMethod1 {
	public static void main(String[] args) {
		double num = 8281;
		double sr = squareRoot(num);
		System.out.format("Square Root of %f is %.2f", num, sr);
	private static double squareRoot(double num) {
		double n2 = num / 2;
		double n1;
		do {
			n1 = n2;
			n2 = (n1 + num / n1) / 2;
		} while (n1 - n2 != 0);
		return n2;


Square Root of 8281.000000 is 91.00
See also  [Solved] error: char cannot be dereferenced