0 points

(Math: approximate the square root)


dashboard

Tue Nov 10 2020 22:42:18 GMT+0000 (UTC)

Posted by @mahmoud hussein #c++

#include <iostream>
#include<cmath>
#include<ctime>
#include<string>
#include <iomanip>
#include <fstream>

using namespace std;



double sqrt(int n);


int main()
{
	
	long n;
	cin >> n;
	cout << "the approximated sqrt of the number " << n << " is " << sqrt(n);
}
double sqrt(long n)
{
	double lastGuess = 1;
	double nextGuess = (lastGuess + n / lastGuess) / 2;

	while (nextGuess - lastGuess > 0.0001)
	{
		nextGuess = lastGuess;
		(lastGuess + (n / lastGuess)) / 2;
	}
	lastGuess = nextGuess;
	return nextGuess = (lastGuess + n / lastGuess) / 2;

}
content_copy Copy

http://cpp.sh/