Monday, July 16, 2018

How do you calculate Square Root of a number in Java?

Problem statement: Find the square root of a number in java !
1st Approach:
public class SquareRootExe {
    public static void main(String args[]) {
        double sqrRoot = squareRoot(4);
        System.out.println(sqrRoot);
    }
    public static double squareRoot(double n) {
        double t;
        double sqRoot = n / 2;
        do {
            t = sqRoot;
            sqRoot = (t + (nt)) / 2;
        } while ((t - sqRoot) != 0);
        return sqRoot;
    }
}
2nd Approach:
public class SquareRootExe {
    public static void main(String args[]) {
        double sqrRoot = squareRoot(4);
        System.out.println(sqrRoot);
    }
    public static double squareRoot(double n) {
        double res = Math.sqrt(n);
        return res;
    }
}
Output:
2.0

No comments:

Post a Comment

How to run standalone mock server on local laptop

 Please download the standalone wiremock server from Direct download section at the bottom of the page.  Download and installation Feel fre...