1. Square Root C

I'm trying make a program using a newton-raphson method this formula run 10 times:

yk+1 = 1/2 . (yk + a/yk)
y1 = 1

The code below have some error, the output have return 0.

Help, thank you!

Code:
#include <stdio.h>
#include <math.h>

int main(void)
{
int k, j, a, b;
float y;

scanf("%d", &a);

for(k = 1; k > a; k++)
{
y = 1;
for(j = 1; 10 < j; j++)
{
y = (y + a/y)/2;
}
}
printf("%d", y);

return 0;
}

2. Quick glance suggest ">" is wrong or k++ is wrong.

Tim S.

Code:
for(k = 1; k > a; k++)

3. continues return 0 if I change > to <

4. You wrote:

printf("%d", y);

y is a float variable, so use %f.

5. Using %f, now return 0.000000

6. I don't think you really understand the Newton-Raphson method. If you can't solve the problem yourself, with paper and pencil, then there is no hope in telling a computer how to do it (i.e. writing a program). You must figure out the math yourself first.

Some basic hints:

• The purpose of it is to find the roots of a function, i.e. where that function crosses the x-axis.
• You seem to be missing the whole derivative part. You must have a function, and have it's derivative.
• Think about the function y = 1 (if that is what you are using -- though it's not clear). Does it make sense to apply the Newton-Raphson method? Why or why not?
• Read the "Practical Considerations" section of the above-linked Wikipedia article, and make sure when you work this out, you pick reasonable initial guesses.

7. The outer loop with k just repeats the same process for the inner loop and is not needed. According to the replies in this thread, this is the code you should have and it's working for me:

Code:
#include <stdio.h>

int main(void)
{
int j, a;
float y;

scanf("%d", &a);

y = 1.;
for(j = 0; j < 10; j++)
{
y = (y + a/y)/2;
}
printf("%f\n", y);

return 0;
}

8. Nevermind my comments in #6. I missed the title of your thread and couldn't figure out what you were trying to do with the N-R method. Makes sense now. Between whiteflags and rcgldr, you should be good to go.

9. Thank you!!!!

10. Thank you all!!

11. The second loop is for to show a table, example: the number and your root, but now I understand.