- //计算某数的平方根
- #include <stdio.h>
- float absoluteValue(float x)
- {
- if(x<0)
- {
- x=-x;
- }
- return x;
- }
- //计算某数平方根的函数
- float squareRoot(float x)
- {
- const float epsilon=.00001;
- float guess=1.0;
- while(absoluteValue(guess*guess-x)>=epsilon)
- {
- guess=(x/guess+guess)/2.0;
- }
- return guess;
- }
- int main()
- {
- float num;
- printf("输入要求平方根的数:");
- scanf("%f",&num);
- printf("%f平方根是%f\n",num,squareRoot(num));
- return 0;
- }