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

Blueprint for self-improvement

To learn faster: Make the process fun To understand yourself : Write To understand the world better : Read To build deeper connection : Lis...