fork download
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. int main() {
  6. double c = 15.0;
  7. double eps = 0.001;
  8.  
  9. double x = c; // przybliżenie początkowe
  10. double x_next;
  11. int iter = 0;
  12.  
  13. do {
  14. x_next = 0.5 * (x + c / x);
  15. iter++;
  16. x = x_next;
  17. } while (fabs(x * x - c) > eps);
  18.  
  19. cout << "Pierwiastek (Newton-Raphson): " << x << endl;
  20. cout << "Liczba iteracji: " << iter << endl;
  21.  
  22. return 0;
  23. }
  24.  
Success #stdin #stdout 0s 5312KB
stdin
Standard input is empty
stdout
Pierwiastek (Newton-Raphson): 3.87298
Liczba iteracji: 5