Home >Backend Development >C++ >How to write n raised to the nth power in c++

How to write n raised to the nth power in c++

下次还敢
下次还敢Original
2024-05-08 02:00:21568browse

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 write n raised to the nth power in c++

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 header file, and the syntax is as follows:

<code class="cpp">double pow(double base, double exponent);</code>

where:

  • base is the number whose power is to be calculated.
  • exponent is a power.

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!

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