-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNewtonsMethod.cpp
More file actions
61 lines (56 loc) · 1.21 KB
/
NewtonsMethod.cpp
File metadata and controls
61 lines (56 loc) · 1.21 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
/*
Randall Hall
C++ program to square root a number using Newton's method
*/
#include <cmath>
#include <iostream>
using namespace std;
double squareRoot(double number)
{
const double ACCURACY = .0000001;
double lower, upper, guess;
if (number < 1)
{
lower = number;
upper = 1;
}
else
{
lower = 1;
upper = number;
}
while ((upper - lower) > ACCURACY)
{
guess = (upper + lower) / 2;
if (guess*guess > number)
upper = guess;
else
lower = guess;
}
return (lower + upper) / 2;
}
int main()
{
double value;
while (value > 0.0 || value < 0.0)
{
do
{
cout << "Enter a number from 0 to 1000: ";
cin >> value;
if (value == 0)
{
break;
}
else
if (value < 0.0 || value > 1000.0)
{
cout << "Number is not in proper range!" << endl;
}
else
cout << "Square root of " << value << " is " << squareRoot(value) << endl;
} while (value < 0.0 || value > 1000.0);
}
cout << "Loop exiting!" << endl;
return 0;
}