平方根.c

来自「都是C语言常用的算法」· C语言 代码 · 共 24 行

C
24
字号
#define Epsilon 1.0E-6  /*控制解的精度*/ 
#include <stdio.h>
#include <math.h> 

main() 
{
	float num,pre,this; 
 	do 
	{   
 		scanf("%f",&num);/*输入要求平方根的数*/ 
	}while(num<0); 
	if (num==0) 
 		printf("the root is 0"); 
	else 
	{
		this=1; 
  		do
  		{ 
  			pre=this;
   			this=(pre+num/pre)/2; 
		}while(fabs(pre-this)>Epsilon);/*用解的精度,控制循环次数,fabs()是求绝对值的函数*/ 
	} 
	printf("the root is %f",this); 
} 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?