c语言程序计算m的n次方及开方
时间: 2024-05-06 07:20:05 浏览: 117
计算m的n次方:
```c
#include <stdio.h>
double power(double m, int n) {
double result = 1.0;
for(int i = 0; i < n; i++) {
result *= m;
}
return result;
}
int main() {
double m, result;
int n;
printf("请输入底数m和指数n:");
scanf("%lf %d", &m, &n);
result = power(m, n);
printf("%.2lf的%d次方为%.2lf", m, n, result);
return 0;
}
```
计算m的开方:
```c
#include <stdio.h>
double sqrt(double m) {
double result = m / 2.0;
double lastResult;
do {
lastResult = result;
result = (result + m / result) / 2.0;
} while (result != lastResult);
return result;
}
int main() {
double m, result;
printf("请输入被开方数m:");
scanf("%lf", &m);
result = sqrt(m);
printf("%.2lf的平方根为%.2lf", m, result);
return 0;
}
```
注:开方使用牛顿迭代法,可以得到更精确的结果。
阅读全文
相关推荐
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)