C++ 中使用 sqrt() 函数求平方根,语法为:double y = sqrt(x);其中 x 为非负数,y 为计算出的平方根值。示例:求 16 的平方根:#include ,double number = 16,double squareRoot = sqrt(number),cout
如何在 C++ 中使用根号
在 C++ 中,可以使用 sqrt() 函数来求一个数的平方根。该函数属于 math 库,需要在程序中包含该头文件:
#include <cmath>
以下是使用 sqrt() 函数求平方根的语法:
double y = sqrt(x);
其中:
立即学习“C++免费学习笔记(深入)”;
示例:
求数字 16 的平方根:
#include <cmath> int main() { double number = 16; double squareRoot = sqrt(number); cout << "平方根为:" << squareRoot << endl; return 0; }
输出:
平方根为:4
以上就是c++++中如何使用根号的详细内容,更多请关注php中文网其它相关文章!
c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号