fork(1) download
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7. double a = 5.0; // liczba, z ktorej liczymy pierwiastek (a > 1)
  8. double x0 = 1.0;
  9. double x1 = a;
  10. double x2;
  11. double eps = 0.0001;
  12.  
  13. // Metoda bisekcji
  14. while ((x1 - x0) > eps) {
  15. x2 = (x0 + x1) / 2.0;
  16.  
  17. if (x2 * x2 > a)
  18. x1 = x2;
  19. else
  20. x0 = x2;
  21. }
  22.  
  23. cout << "Przyblizona wartosc sqrt(" << a << ") = " << x2 << endl;
  24.  
  25. return 0;
  26. }
  27.  
Success #stdin #stdout 0.01s 5324KB
stdin
Standard input is empty
stdout
Przyblizona wartosc sqrt(5) = 2.23602