mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 01:35:16 +08:00
update
added comments and variable names are more simpler now
This commit is contained in:
@ -7,22 +7,27 @@ import java.util.regex.*;
|
|||||||
public class Solution {
|
public class Solution {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
//take input
|
||||||
Scanner scn = new Scanner(System.in);
|
Scanner scn = new Scanner(System.in);
|
||||||
|
|
||||||
int N = scn.nextInt();
|
int N = scn.nextInt(); //N is the input number
|
||||||
int P = scn.nextInt();
|
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));
|
System.out.println(squareRoot(N, P));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static double squareRoot(int N, int P) {
|
public static double squareRoot(int N, int P) {
|
||||||
double sqrt = 0;;
|
double rv = 0; //rv means return value
|
||||||
|
|
||||||
// Write your code here
|
|
||||||
double root = Math.pow(N, 0.5);
|
double root = Math.pow(N, 0.5);
|
||||||
int pre = (int) Math.pow(10, P);
|
|
||||||
root = root * pre;
|
//calculate precision to power of 10 and then multiply it with root value.
|
||||||
sqrt = (int)root;
|
int precision = (int) Math.pow(10, P);
|
||||||
return (double)sqrt/pre;
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user