#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; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter