迭代法求平方根

来源:互联网 发布:淘宝开放平台难吗 编辑:程序博客网 时间:2024/05/16 14:38
#include <stdio.h>#include <stdlib.h>#include <math.h>int main(){    float a,x,y,t;    printf("Please input a:");    scanf("%f",&a);    t=1;    x=1; //对x,t 赋初值是非常重要的,若不赋初值,则会给x,t随机数    while(t>=1e-5)    {        y=0.5*(x+a/x);        t=fabs(y-x);        x=y;    }    printf("%f\n",y);    return 0;}



原创粉丝点击