在C語言中,開根號的代碼是“sqrt(浮點值x)”;例如“sqrt(4.0)”,對4進行平方根運算,結(jié)果為2。sqrt()是c語言內(nèi)置的開根號運算函數(shù),其運算結(jié)果是函數(shù)變量的算術(shù)平方根;該函數(shù)既不能運算負數(shù)值,也不能輸出虛數(shù)結(jié)果。
本教程操作環(huán)境:windows7系統(tǒng)、C++17版本、Dell G3電腦。
在頭文件中加入#include<math.h>
之后使用sqrt即可,可以用double定義
舉個例子:
#include <math.h> #include <stdio.h> #include <stdlib.h> double sqrt(double n); int main(void) { double x = 3.1415926540, result; result = sqrt(x); printf("The square root of %lf is %lf", x, result); return 0; }
推薦教程:《C#》