Square root in C using Newton-Raphson method

后端 未结 4 699
一向
一向 2021-01-16 10:08

In the following code, I want to replace the termination condition to: if the ratio of guess square and x is close to 1, while loop should terminate. I tried various express

4条回答
  •  情书的邮戳
    2021-01-16 11:10

    # include
    
    double sq_root(double x)
    {
        double rt = 1, ort = 0;
        while(ort!=rt)
        {
            ort = rt;
            rt = ((x/rt) + rt) / 2;
        }
        return rt;
    }
    
    int main(void)
    {
        int i;
        for(i = 2; i<1001; i++) printf("square root of %d is %f\n",i, sq_root(i));
        return 0;
    }
    

提交回复
热议问题