显示巴比伦根的步骤

时间:2017-09-28 23:34:17

标签: c loops while-loop sqrt

所以我将所有代码设置到它将给我平方根的位置,并且我有用户输入的初始数字,你想要平方根和用户输入猜测。

我需要使用while循环来显示获取数字的平方根所需的步数,并显示“获取(用户输入值)的平方根需要(但需要很多步骤) (SQRT)“

格式:

Enter a number: 72
Enter a starting guess: 36
The square root of 72.000000 is 8.485281

step 1: 19.000000
step 2: 11.394737
step 3: 8.856722
step 4: 8.493070
step 5: 8.485285
step 6: 8.485281
Using the Babylonian method, the square root of 72
with a starting guess of 36 was found in 6 steps.

还包括这个猜测公式,但我不明白它的用途:

guessk+1 = ½(guessk + (N/guessk))

到目前为止,我的代码是:

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

int main(void) {

        double N;
        double guess;
        int stp;

        printf("Enter a number: ");
        scanf("%lf", &N);

        printf("\nEnter a starting guess: ");
        scanf("%lf", &guess);

        printf("The square root of %lf is %lf\n\n", N, sqrt(N));

        return (0);
}

0 个答案:

没有答案