From 32d03516fe647d47655e16676bc114a9428083cd Mon Sep 17 00:00:00 2001 From: Deepak Date: Tue, 3 Oct 2017 13:12:50 +0530 Subject: [PATCH] update added comments and variable names are more simpler now --- Misc/root_precision.java | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/Misc/root_precision.java b/Misc/root_precision.java index 1b5e95478..0ae00de0f 100644 --- a/Misc/root_precision.java +++ b/Misc/root_precision.java @@ -7,22 +7,27 @@ import java.util.regex.*; public class Solution { public static void main(String[] args) { + //take input Scanner scn = new Scanner(System.in); - int N = scn.nextInt(); - int P = scn.nextInt(); + int N = scn.nextInt(); //N is the input number + int P = scn.nextInt(); //P is precision value for eg - P is 3 in 2.564 and 5 in 3.80870. System.out.println(squareRoot(N, P)); } public static double squareRoot(int N, int P) { - double sqrt = 0;; - - // Write your code here - double root = Math.pow(N, 0.5); - int pre = (int) Math.pow(10, P); - root = root * pre; - sqrt = (int)root; - return (double)sqrt/pre; + double rv = 0; //rv means return value + + double root = Math.pow(N, 0.5); + + //calculate precision to power of 10 and then multiply it with root value. + int precision = (int) Math.pow(10, P); + root = root * precision; + /*typecast it into integer then divide by precision and again typecast into double + so as to have decimal points upto P precision */ + + rv = (int)root; + return (double)rv/precision; } }