Home >Backend Development >C++ >How to write n raised to the nth power in c++
Compute n to the power of n in C: Use the pow() function, located in the
header file. pow(base, exponent), where base is the number to be raised to a power and exponent is the power. For example, pow(3, 4) calculates 3 raised to the 4th power, which is 81.
How to calculate n raised to the power of n in C
In C, you can use pow() The function computes n raised to the nth power. This function is located in the
<code class="cpp">double pow(double base, double exponent);</code>
where:
Usage:
To calculate n raised to the power n, use the following code:
<code class="cpp">double result = pow(n, n);</code>
Example:
The following is an example showing how to calculate 3 raised to the 4th power:
<code class="cpp">#include <iostream> #include <cmath> using namespace std; int main() { int n = 3; double result = pow(n, n); cout << "3 的 4 次方为:" << result << endl; return 0; }</code>
Output:
<code>3 的 4 次方为:81</code>
The above is the detailed content of How to write n raised to the nth power in c++. For more information, please follow other related articles on the PHP Chinese website!