Search⌘ K
AI Features

Solution: Newton's Method

Explore how to apply Newton's method in C to approximate the square root of a number. Learn to use for loops, conditional checks, and break statements for iterative improvement and early loop exit, enhancing your understanding of control flow and numerical methods in programming.

We'll cover the following...
C
#include <stdio.h>
int main(void)
{
double num = 612;
double x1 = 10;
double xi;
int i;
for (i = 1; i <= 5; i++) {
// No need to continue if the exact solution has been found
if (x1 * x1 - num == 0) {
break;
}
// Find a better approximation
xi = x1 - ( ((x1 * x1) - num) / (2 * x1) );
printf("x%d = %.12f, x%d = %.12f\n", i, x1, i + 1, xi);
x1 = xi; // x1 is now the new approximation.
}
return 0;
}
...