111的111次方求个位十位百位的c加加程序
时间: 2024-10-14 22:12:15 浏览: 33
C语言程序设计-编写函数fun,求任一整数m的n次方;.c
5星 · 资源好评率100%
要编写一个C语言程序来计算111的111次方,并得到结果的个位、十位和百位数字,可以利用取模运算来分步计算每个位置上的值,因为取模可以保持结果在较小范围内,便于查看每一位。下面是一个简单的示例:
```c
#include <stdio.h>
#include <string.h>
// Function to calculate the digit at a given position in the number
char getDigitAtPosition(int num, int pos) {
// For 111^111, the result will always be 1 because of the cyclicity of powers of 111
return (num % 10); // The last digit is the remainder when divided by 10
}
int main() {
int base = 111;
int exponent = 111;
// Since we know 111 raised to any power will have only one cycle and that cycle will start with 1,
// directly calculating the power will give us the required result for each digit.
int result = pow(base, exponent);
char unit_digit = getDigitAtPosition(result, 0); // Get the last digit (unit place)
char ten_digit = getDigitAtPosition(result, 1); // Get the second-to-last digit (tens place)
char hundred_digit = getDigitAtPosition(result, 2); // Get the third-to-last digit (hundreds place)
printf("The last digit (unit): %d\n", unit_digit);
printf("The second-to-last digit (tens): %d\n", ten_digit);
printf("The third-to-last digit (hundreds): %d\n", hundred_digit);
return 0;
}
```
注意:这里使用了`pow`函数来进行幂运算,但在实际编程中,由于`pow`属于C99标准,不是所有旧版的编译器都支持。若不支持,可以使用循环实现幂运算。
阅读全文