C pow()

pow() 函数接收两个参数(基数和幂数),并返回基数的幂次。例如:

[Mathematics] xy = pow(x, y) [In programming]

pow() 函数在 math.h 头文件中定义。


C pow() 原型

double pow(double x, double y)

第一个参数是基数值,第二个参数是基数的幂次。

要计算 intfloat 变量的幂,可以使用类型转换运算符显式转换为 double

int base = 3;
int power = 5;
pow(double(base), double(power));

示例:C pow() 函数

#include <stdio.h>
#include <math.h>

int main()
{
    double base, power, result;

    printf("Enter the base number: ");
    scanf("%lf", &base);

    printf("Enter the power raised: ");
    scanf("%lf",&power);

    result = pow(base,power);

    printf("%.1lf^%.1lf = %.2lf", base, power, result);

    return 0;
}

输出

Enter the base number: 2.5
Enter the power raised: 3.4
2.5^3.4 = 22.54

在结束之前,让我们来测试一下你对 C math pow() 的了解!你能解决下面的挑战吗?

挑战

编写一个函数来计算给定数字的平方。

  • 例如,输入 num = 3,返回值应为 8
你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战