I have this code that should calculate the square root of a number useing newton's method.
It compiles and runs , but while I debug ,it doesn't give the right answer for most numbers (except for the roots of 4,16,25, 36 etc)thanks for any help !
Code:#include <stdio.h> int squareRoot(int num,int ans,int tol); int main(void) { int num,ans,tol,root; printf("Enter the number you want squarerooted"); scanf("%d",&num); printf("Enter your guess of the number's root"); scanf("%d",&ans); printf("Enter the pretidetermined tolerance"); scanf("%d",&tol); root = squareRoot(num,ans,tol); printf("%d%d","The square root of ",num," is:",root); } int squareRoot(int num,int ans,int tol) { if((ans^2 - num <= tol)) return ans; else return (squareRoot(num,((ans^2 + num)/(2 * ans)),tol)); }



LinkBack URL
About LinkBacks



I did notice though when debugging ,and I enter say 50 for num , 7 for ans , and 0.01 for tol , it just returns 7.
Thanks again