in the following program i am trying to compte the square root usiing a function. everything works except i keep getting the number one printed out as the square root of any number. here is the code, thanks for your help!
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
main() {
double square_root (double, double);
double tolerance;
double input;
double ans;
printf("Input tolerance for Newton's method: ");
scanf("%le", &tolerance);
printf("Input a non-negative number: ");
scanf("%le", &input);
ans = square_root (input, tolerance);
printf("The square root of: %1.15lf is %1.15lf\n", input, ans);
}
//function
double square_root (double i, double t) {
double r = 1;
while( fabs((r*r)-i) <= t ) {
r = (i/(r + r)) / 2;
}
return r;
}
Code tags added by Kermi3