利用c语言函数求100-1000以内的回文数个数
时间: 2023-08-01 16:09:28 浏览: 184
C语言回文数
5星 · 资源好评率100%
可以利用以下的函数来判断一个数是否为回文数:
```c
int isPalindrome(int n) {
int reversed = 0, remainder, original = n;
while (n != 0) {
remainder = n % 10;
reversed = reversed * 10 + remainder;
n /= 10;
}
return original == reversed;
}
```
接下来,我们可以在主函数中遍历100-1000之间的每个数,然后调用这个函数判断它是否为回文数。如果是回文数,我们就增加回文数的计数器。
完整代码如下:
```c
#include <stdio.h>
int isPalindrome(int n) {
int reversed = 0, remainder, original = n;
while (n != 0) {
remainder = n % 10;
reversed = reversed * 10 + remainder;
n /= 10;
}
return original == reversed;
}
int main() {
int count = 0;
for (int i = 100; i <= 1000; i++) {
if (isPalindrome(i)) {
count++;
}
}
printf("There are %d palindromes between 100 and 1000.\n", count);
return 0;
}
```
阅读全文