c语言中10的n次方怎么打
时间: 2024-10-12 12:17:17 浏览: 94
c语言中x的n次方怎么写
在C语言中,计算10的n次方通常不直接支持内置的运算符,因为没有直接提供幂运算。但是你可以通过循环或递归来实现这个功能。以下是两种常见的方法:
1. **循环法** (for loop):
```c
#include <stdio.h>
int power(int base, int exponent) {
int result = 1;
for (int i = 0; i < exponent; i++) {
result *= base;
}
return result;
}
int main() {
int n;
printf("Enter the value of n: ");
scanf("%d", &n);
printf("10 to the power of %d is %d\n", n, power(10, n));
return 0;
}
```
2. **递归法** (recursive function):
```c
#include <stdio.h>
int power(int base, int exponent) {
if (exponent == 0)
return 1;
else
return base * power(base, exponent - 1);
}
int main() {
int n;
printf("Enter the value of n: ");
scanf("%d", &n);
printf("10 to the power of %d is %d\n", n, power(10, n));
return 0;
}
```
阅读全文