Home  >  Article  >  Backend Development  >  How to express the nth power of a in C language

How to express the nth power of a in C language

下次还敢
下次还敢Original
2024-04-27 22:09:371197browse

In C language, the pow() function is used to calculate the nth power of a. The base a needs to be used as base and the exponent n needs to be passed to the function as exponent. For example, to calculate 2 raised to the 5th power, call pow(2, 5).

How to express the nth power of a in C language

The nth power of a in C language

In C language, use the pow() function to calculate a to the nth power. The prototype of this function is:

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

where:

  • base: base a
  • exponent: exponent n

To calculate a To the power n, you simply pass a as the base and n as the exponent to the pow() function. For example:

<code class="c">#include <stdio.h>
#include <math.h>

int main() {
  // 计算 2 的 5 次方
  double result = pow(2, 5);

  // 输出结果
  printf("2 的 5 次方是 %f\n", result);

  return 0;
}</code>

Output:

<code>2 的 5 次方是 32.000000</code>

Note:

    ##pow() function returns a double type value.
  • If exponent is negative, the function will return NaN (not a number).
  • If base is 0 and exponent is negative, this function will return infinity.

The above is the detailed content of How to express the nth power of a in C language. 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