Home  >  Article  >  Backend Development  >  How to use root sign in c++

How to use root sign in c++

下次还敢
下次还敢Original
2024-05-01 14:24:13937browse

Use the sqrt() function in C to find the square root. The syntax is: double y = sqrt(x); where x is a non-negative number and y is the calculated square root value. Example: Find the square root of 16: #include , double number = 16, double squareRoot = sqrt(number), cout << "The square root is:" << squareRoot << endl; .

How to use root sign in c++

How to use the root sign in C

In C, you can use sqrt() Function to find the square root of a number. This function belongs to the math library, and the header file needs to be included in the program:

<code class="cpp">#include <cmath></code>

The following is the syntax for using the sqrt() function to find the square root:

<code class="cpp">double y = sqrt(x);</code>

Where:

  • x is the number to calculate the square root (must be a non-negative number).
  • y is the calculated square root value.

Example:

Find the square root of the number 16:

<code class="cpp">#include <cmath>

int main() {
  double number = 16;
  double squareRoot = sqrt(number);
  cout << "平方根为:" << squareRoot << endl;

  return 0;
}</code>

Output:

<code>平方根为:4</code>

The above is the detailed content of How to use root sign in c++. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How to use printf in c++Next article:How to use printf in c++