-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarysqureroot.cpp
More file actions
72 lines (56 loc) · 1 KB
/
Copy pathbinarysqureroot.cpp
File metadata and controls
72 lines (56 loc) · 1 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include<iostream>
using namespace std;
int binarysquare(int array[], int k, int s)
{
int end = s-1;
int start = 0;
int ans = -1;
while(start <= end)
{
int mid = (start + end)/2;
int square = array[mid] * array[mid];
if(square == k )
{
ans = array[mid];
break;
}
if(square > k)
{
end = mid-1;
}
if(square < k)
{
ans = array[mid];
start = mid+1;
}
}
return ans;
}
double precised(double ans, int n)
{
double factor = 1;
for(int i = 0; i < 3; i++)
{
factor = factor / 10;
for(double j = ans; j*j < n; j = j+factor)
{
ans = j;
}
}
return ans;
}
int main()
{
int array[1000];
int num;
cout<<" enter number"<<endl;
cin >> num;
for(int i = 0; i < 1000; i++)
{
array[i] = i + 1;
};
int squareroot = binarysquare(array, num, 1000);
double moreprecised = precised(squareroot, num);
cout<<" squareroot "<<squareroot <<endl <<" decimal point " << moreprecised;
return 0;
}