Lab 5

Lab Description
We had to create a C++ program the will approximate the square root of a number using the Babylonian Algorithm.
Displaying partial results as you approximate the square root up until the difference is less then the tolerance.
When doing the code i could not get it to display the the difference between the square root and Babylonian as a
decimal.

Code

#include 
#include 
#include 
using namespace std;

int main()
{
	double x, guess, r, y, last,diff;
	double t = .00001;
	cout <> x;

	guess = x / 2;
	y = sqrt(x);
		do
		{
			r = x / guess;
			guess = (guess + r) / 2;
			diff = abs(guess - r);
			cout << " The sqrt is " << guess << endl;
			cout << " The difference is " << diff << endl;
			cout < t);
		cout << " sqrt of input is " << y << endl;
		cout << " The difference between sqrt and Babylonian is " << diff - y << endl;
}

ScreenShot
Untitled

Untitled

Leave a Reply

Your email address will not be published. Required fields are marked *