2019-05-09 19:32:54 +08:00
|
|
|
package Others;
|
|
|
|
|
2019-02-05 13:03:37 +08:00
|
|
|
import java.util.Scanner;
|
2017-10-02 15:30:39 +08:00
|
|
|
|
2017-12-14 10:57:43 +08:00
|
|
|
public class RootPrecision {
|
2017-10-02 15:30:39 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
public static void main(String[] args) {
|
|
|
|
// take input
|
|
|
|
Scanner scn = new Scanner(System.in);
|
2019-02-05 13:03:37 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
// N is the input number
|
|
|
|
int N = scn.nextInt();
|
2019-02-05 13:03:37 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
// P is precision value for eg - P is 3 in 2.564 and 5 in 3.80870.
|
|
|
|
int P = scn.nextInt();
|
|
|
|
System.out.println(squareRoot(N, P));
|
2020-01-29 00:34:52 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
scn.close();
|
|
|
|
}
|
2019-02-05 13:03:37 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
public static double squareRoot(int N, int P) {
|
|
|
|
// rv means return value
|
|
|
|
double rv;
|
2019-02-05 13:03:37 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
double root = Math.pow(N, 0.5);
|
2019-02-05 13:03:37 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
// 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 */
|
2019-02-05 13:03:37 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
rv = (int) root;
|
|
|
|
return rv / precision;
|
|
|
|
}
|
|
|
|
}
|