blob: d75bd9e757768e1d850e6ab6969a78f724e95aaa (
plain) (
blame)
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
|
#include <iostream>
#include <cmath>
using namespace std;
double gennewGuess(double n, double old_guess) {
bool WA=true;
double real, old;
//if (WA) {
real=sqrt(n);
WA=false;
//}
double new_guess = ( old_guess + n / old_guess ) / 2;
old=new_guess;
if (((new_guess-real)/real)>0.01) {
cout << old << endl;
return gennewGuess(n, old);
} else {
cout << old << endl;
return new_guess;
}
}
int main () {
double temp, temp2;
cout << "Enter the Number to find square root: ";
cin >> temp;
if (temp<=0) {
cout << "Please enter a valid input(Positive Integer)." << endl;
return -1;
}
cout << "Guessing..." << endl;
double real=sqrt(temp);
temp2=temp;
temp = gennewGuess(temp, 1.0);
cout << "The Final Guess: " << temp << endl
<< "Actual Value: " << real << endl;
}
|