
从用户那里获取两个整数作为底数和指数,并按照下面的说明计算幂。
示例
考虑以下内容以编写一个C程序。
- 假设底数为3
- 指数为4
- 幂=3*3*3*3
算法
按照下面给出的算法进行操作:
Step 1: Declare int and long variables. Step 2: Enter base value through console. Step 3: Enter exponent value through console. Step 4: While loop. Exponent !=0 i. Value *=base ii. –exponent Step 5: Print the result.
示例
以下程序解释了如何用 C 语言计算给定数字的幂。
婚纱影楼小程序提供了一个连接用户与影楼的平台,相当于影楼在微信的官网。它能帮助影楼展示拍摄实力,记录访客数据,宣传优惠活动。使用频率高,方便传播,是影楼在微信端宣传营销的得力助手。功能特点:样片页是影楼展示优秀摄影样片提供给用户欣赏并且吸引客户的。套系页是影楼根据市场需求推出的不同套餐,用户可以按照自己的喜好预定套系。个人中心可以查看用户预约的拍摄计划,也可以获取到影楼的联系方式。
#includeint main(){ int base, exponent; long value = 1; printf("Enter a base value: "); scanf("%d", &base); printf("Enter an exponent value: "); scanf("%d", &exponent); while (exponent != 0){ value *= base; --exponent; } printf("result = %ld", value); return 0; }
输出
当执行上述程序时,会产生以下结果 -
Run 1: Enter a base value: 5 Enter an exponent value: 4 result = 625 Run 2: Enter a base value: 8 Enter an exponent value: 3 result = 512
示例
如果我们想要找到实数的幂,我们可以使用 pow 函数,它是 math.h 中的一个预定义函数。
#include#include int main() { double base, exponent, value; printf("Enter a base value: "); scanf("%lf", &base); printf("Enter an exponent value: "); scanf("%lf", &exponent); // calculates the power value = pow(base, exponent); printf("%.1lf^%.1lf = %.2lf", base, exponent, value); return 0; }
输出
当执行上述程序时,会产生以下结果 -
Enter a base value: 3.4 Enter an exponent value: 2.3 3.4^2.3 = 16.69









