Home >Backend Development >C#.Net Tutorial >How to express the nth power of x in C language

How to express the nth power of x in C language

下次还敢
下次还敢Original
2024-04-29 19:03:14588browse

In C language, you can use the pow() function to calculate the nth power of x. Its syntax is: pow(double x, double y); it uses x as the base, y as the exponent, and returns x to the power of y.

How to express the nth power of x in C language

In C language, the nth power of x represents

In C language, you can use the pow() function Calculates x raised to the nth power.

pow() function syntax

<code class="c">double pow(double x, double y);</code>

Parameter description

  • x: base
  • y: Exponent

Return value

Returns the result of x raised to the power of y. If y is an integer, it returns the double type, otherwise it returns the long double type. .

Code example

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

int main() {
    double x, n;

    printf("请输入底数 x:");
    scanf("%lf", &x);
    printf("请输入指数 n:");
    scanf("%lf", &n);

    double result = pow(x, n);
    printf("x 的 n 次幂为:%.2f\n", result);

    return 0;
}</code>

Output example

<code>请输入底数 x:5
请输入指数 n:3
x 的 n 次幂为:125.00</code>

The above is the detailed content of How to express the nth power of x 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