fork download
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. double a; // liczba dodatnia
  9. double x; // przybliżenie
  10. double eps = 1e-6;
  11.  
  12. cout << "Podaj dodatnia liczbe rzeczywista: ";
  13. cin >> a;
  14.  
  15. if (a <= 0)
  16. {
  17. cout << "Liczba musi byc dodatnia!" << endl;
  18. return 1;
  19. }
  20.  
  21. // Przybliżenie początkowe
  22. x = a;
  23.  
  24. // Iteracje Newtona-Raphsona
  25. while (fabs(x * x - a) > eps)
  26. {
  27. x = 0.5 * (x + a / x);
  28. }
  29.  
  30. cout << "Przyblizona wartosc sqrt(" << a << ") = " << x << endl;
  31.  
  32. return 0;
  33. }
  34.  
Success #stdin #stdout 0s 5316KB
stdin
3
stdout
Podaj dodatnia liczbe rzeczywista: Przyblizona wartosc sqrt(3) = 1.73205