Lab 5

Today I made a c++ program that will approximate the square root of a number using the Babylonian Algorithm explained in class. It will display the approximated square root as partial results. Then after approximating the square root up to the tolerance which was given I compared it with the result obtained by using the sqrt( ) function in the math.h library created.

#include
#include
using namespace std;

	
	double sqr00t(double n) {
	int count =0;
	double x = n;
	double y = 1;
	while(x - y > 0.000001)
	 {
		y = n/x;
		x = (x + y)/2;
		count++;
		cout<<"Iteration #:"<<count<<endl;
		cout<<"Approx. Babylonian square root value is: "<<x<<endl;
		cout<<"Difference is:"<<x-y<<endl;
		cout<<endl;
	}
	cout<<endl; cout<<"Babylonian square root value "<<n<<" is "<<x<<endl;
		return x;
						}
	int main() { double n;
        cout<>n;
	sqr00t(n);
	cout<<"using sqrt, the result is:"<<sqrt(n)<<endl;
	cout<<"Difference between sqrt and Babylonian is:"<<sqr00t(n)-sqrt(n)<<endl;
	return 0;
	}

lab5

lab5_code screenshot

Leave a Reply

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