fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. int n;
  6. double r, intGuess, guess, ratio;
  7.  
  8. // user input
  9. cout << "Enter number: ";
  10. cin >> n;
  11.  
  12. intGuess = n;
  13. guess = n / 2;
  14. ratio = intGuess / guess;
  15.  
  16. while ( ratio >= 1.01 || ratio <= 0.99 )
  17. {
  18. intGuess = guess;
  19. r = n / guess;
  20. guess = (guess + r) / 2;
  21. ratio = intGuess / guess;
  22. }
  23.  
  24. cout << endl;
  25. cout << "The square root of " << n << " is " << guess << endl;
  26. return 0;
  27. }
Success #stdin #stdout 0s 3344KB
stdin
9
stdout
Enter number: 
The square root of 9 is 3