Lab 5

In this lab we had to create a C++ program that would approximate the square root of a number using the Babylonian Algorithm. Display partial results as it approximates the square root. After approximating the square root up to the tolerance, it compares it with the result obtained by using the sqrt( ) function.

#include 
#include 
#include 
using namespace std;

int main()
{
	double a, r, b, c;
	double t = .00001

	cout << 0 " Enter a number "; 	
 cin >> a;

	b = a / 2;
	b = sqrt( a )

		do
		{
			r = a / b;
			b = (b + r) / 2;
			c = abs(b - r);
			cout << " The square root is " << b << endl;
			cout << " The Difference is " << c << endl;
			cout << endl;
		} while (c <= t );

	cout << " The square root of input is " << b << endl;
	cout << " The difference between the square root and Babylonian is " << c - b << endl;1

Untitled1

Leave a Reply

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