Skip to content

Instantly share code, notes, and snippets.

@snghnishant
Created April 24, 2020 07:12
Show Gist options
  • Save snghnishant/cf67940b9ee93f458707f259cf586f22 to your computer and use it in GitHub Desktop.
Save snghnishant/cf67940b9ee93f458707f259cf586f22 to your computer and use it in GitHub Desktop.
Newton Square Root
#include <iostream>
#include<math.h>
using namespace std;
int main() {
float guess, quotient, average{0};
int num, x{1};
cin>>num;
while(true){
if(pow(x,2)<=num){
guess = x;
x++;
}else{
break;
}
}
int i{5};
while(i--){
quotient = static_cast<float> (num/guess);
average = (guess+quotient)/2;
cout<<guess<<" "<<quotient<<" "<<average<<endl;
guess = average;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment