Skip to content

Commit d1475f1

Browse files
Merge pull request TheAlgorithms#127 from dpacmen/patch-2
add precision_root_algorithm
2 parents 4d6e69d + 32d0351 commit d1475f1

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

Misc/root_precision.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.io.*;
2+
import java.util.*;
3+
import java.text.*;
4+
import java.math.*;
5+
import java.util.regex.*;
6+
7+
public class Solution {
8+
9+
public static void main(String[] args) {
10+
//take input
11+
Scanner scn = new Scanner(System.in);
12+
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.
15+
16+
System.out.println(squareRoot(N, P));
17+
}
18+
19+
public static double squareRoot(int N, int P) {
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;
32+
}
33+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy