File tree 1 file changed +15
-10
lines changed
1 file changed +15
-10
lines changed Original file line number Diff line number Diff line change 7
7
public class Solution {
8
8
9
9
public static void main (String [] args ) {
10
+ //take input
10
11
Scanner scn = new Scanner (System .in );
11
12
12
- int N = scn .nextInt ();
13
- int P = scn .nextInt ();
13
+ int N = scn .nextInt (); //N is the input number
14
+ int P = scn .nextInt (); //P is precision value for eg - P is 3 in 2.564 and 5 in 3.80870.
14
15
15
16
System .out .println (squareRoot (N , P ));
16
17
}
17
18
18
19
public static double squareRoot (int N , int P ) {
19
- double sqrt = 0 ;;
20
-
21
- // Write your code here
22
- double root = Math .pow (N , 0.5 );
23
- int pre = (int ) Math .pow (10 , P );
24
- root = root * pre ;
25
- sqrt = (int )root ;
26
- return (double )sqrt /pre ;
20
+ double rv = 0 ; //rv means return value
21
+
22
+ double root = Math .pow (N , 0.5 );
23
+
24
+ //calculate precision to power of 10 and then multiply it with root value.
25
+ int precision = (int ) Math .pow (10 , P );
26
+ root = root * precision ;
27
+ /*typecast it into integer then divide by precision and again typecast into double
28
+ so as to have decimal points upto P precision */
29
+
30
+ rv = (int )root ;
31
+ return (double )rv /precision ;
27
32
}
28
33
}
You can’t perform that action at this time.
0 commit comments