There are two ways to express nth power in C: 1. pow() function; 2. ^ operator. The pow() function is located in the
header file, and the ^ operator has higher precedence than * and /, but lower than and -.
Power operation in C
How to express nth power in C?
There are two ways to express nth power in C:
1. Use the pow() function
<code class="cpp">#include <cmath> double result = pow(base, exponent);</code>
where:
base
is the base. exponent
is the exponent. result
is the result. 2. Use ^ operator
<code class="cpp">double result = base ^ exponent;</code>
Example:
Calculate 2 5th power:
Use pow()
Function:
<code class="cpp">#include <cmath> double result = pow(2, 5);</code>
Use ^ Operator:
<code class="cpp">double result = 2 ^ 5;</code>
Note:
and
/, but lower than
and
-. Therefore, you need to be careful when using the
^ operator. The
The above is the detailed content of How to express nth power in c++. For more information, please follow other related articles on the PHP Chinese website!