-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqaurRootIntegralAndPrecision.java
More file actions
55 lines (48 loc) · 1.09 KB
/
SqaurRootIntegralAndPrecision.java
File metadata and controls
55 lines (48 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package june16;
import java.util.Scanner;
public class SqaurRootIntegralAndPrecision {
public static int sqrt1(int n){
if(n==0||n==1)
return n;
int res=1,i=1;
while(res<=n){
i++;
res=i*i;
}
return i-1;
}
public static double sqrt2(int n,int p){
if(n==0||n==1)
return n;
double ans=1;
int i=1;
while(ans<=n){
i++;
ans=i*i;
}
ans=i-1;
float increment=(float)(0.1);
int pow=1;
double pow2=1.0;
for(int j=0;j<p;j++){
while(ans*ans<=n){
ans=ans+increment;
}
ans=ans-increment;
pow*=10;
pow2*=10.0;
increment=increment/10;
}
return (Math.round(ans*pow)/pow2);
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
//int n=27;
int p=sc.nextInt();
//int p=2;
int res1=sqrt1(n);
double res2=sqrt2(n,p);
System.out.println(res1+" "+res2);
}
}