Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- using namespace std;
- int main()
- {
- double x, precision;
- cout << "Enter X: ";
- cin >> x;
- if (x <= 0) {
- cout << "X must be positive and larger than 0. Can't continue. Farewell.\n\n";
- return 0;
- }
- else {
- cout << "Enter precision: ";
- cin >> precision;
- double low = 1;
- double high = x;
- double mid = 0;
- while (fabs(mid * mid - x) > precision) {
- mid = low + (high - low) / 2;
- if (mid * mid > x) {
- high = mid;
- }
- else {
- low = mid;
- }
- }
- cout << "Square root of " << x << " is: " << mid << endl << endl;
- //check here cout << "sqrt of " << x << " is: " << sqrt(x) << endl << endl;
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement