C语言 编写一个计算2023的2021次幂对37的余数
时间: 2023-05-20 20:05:22 浏览: 51
可以使用快速幂算法来计算,以下是C语言代码:
```c
#include <stdio.h>
int pow_mod(int a, int b, int m) {
int res = 1 % m;
while (b) {
if (b & 1) res = (long long)res * a % m;
a = (long long)a * a % m;
b >>= 1;
}
return res;
}
int main() {
int ans = pow_mod(2023, 2021, 37);
printf("%d\n", ans);
return 0;
}
```
输出结果为:21
相关问题
用c语言写一个计算正整数n次幂的函数
好的,我可以帮您解决这个问题。以下是用 C 语言编写一个计算正整数 n 次幂的函数的示例代码:
```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 base, exponent;
printf("请输入底数:");
scanf("%d", &base);
printf("请输入指数:");
scanf("%d", &exponent);
int result = power(base, exponent);
printf("%d 的 %d 次幂为 %d。\n", base, exponent, result);
return 0;
}
```
这个函数使用了循环来计算 n 次幂,循环的次数是指数的值。代码中的 main 函数通过用户输入底数和指数来计算幂,然后输出结果。
希望这个代码示例能帮到您!如果您还有其他问题,请随时提出。
C语言编写一个计算圆柱的体积
好的,我可以帮您解决关于C语言编写计算圆柱体积的问题。下面是一个简单的C程序,可以计算圆柱的体积。
```
#include <stdio.h>
int main()
{
float r; // 圆的半径
float h; // 圆柱的高
float s; // 圆的面积
float v; // 圆柱的体积
printf("请输入圆的半径:");
scanf("%f", &r);
printf("请输入圆柱的高:");
scanf("%f", &h);
// 计算圆的面积
s = 3.14 * r * r;
// 计算圆柱的体积
v = s * h;
printf("圆柱的体积为:%.2f", v);
return 0;
}
```
您可以将该代码保存为一个.c文件,然后使用C编译器进行编译和运行。当程序运行时,会提示您输入圆的半径和圆柱的高,然后计算圆柱的体积并输出。希望这个程序对您有所帮助。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)