编写C语言程序打印 10 ~ 1000 之间满足下列条件的所有整数正序读和反序读都相同
时间: 2023-03-11 20:48:44 浏览: 323
我可以提供一个简单的C语言程序来打印10到1000之间满足条件的所有整数:#include <stdio.h>
#include <string.h>int main()
{
int i;
for(i=10;i<=1000;i++)
{
char str[4];
sprintf(str, "%d", i);
if(strcmp(str, strrev(str)) == 0)
{
printf("%d\n", i);
}
}
return 0;
}
相关问题
c语言打印 10~1000 之间满足下列条件的所有整数: 正序读和反序读都相同代码
以下是 C 语言代码实现:
```c
#include <stdio.h>
int main() {
int num;
for(num = 10; num <= 1000; num++) {
int temp = num, reverse = 0, digit;
while(temp > 0) {
digit = temp % 10;
reverse = reverse * 10 + digit;
temp /= 10;
}
if(num == reverse) {
printf("%d\n", num);
}
}
return 0;
}
```
这段代码会遍历 10~1000 之间的所有整数,对于每个整数,通过取余和除法得到这个整数的反序数,并与原数比较是否相等。如果相等,则说明这个整数是正序读和反序读都相同的整数,将其输出即可。
利用c语言编程打印10~1000之间满足下列条件的所有整数:正序读和反序读都相同,例如:55、232等。
好的,下面是C语言的代码实现:
```c
#include <stdio.h>
int isPalindrome(int num) {
int temp = num, reversed = 0;
while (temp != 0) {
reversed = reversed * 10 + temp % 10;
temp /= 10;
}
return reversed == num;
}
int main() {
for (int i = 10; i <= 1000; i++) {
if (isPalindrome(i)) {
printf("%d ", i);
}
}
printf("\n");
return 0;
}
```
代码中的isPalindrome函数用于判断一个数是否为回文数,即正序读和反序读都相同。在主函数中,通过一个for循环遍历10到1000之间的所有整数,将每个数传入isPalindrome函数中进行判断,若为回文数则打印输出。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://img-home.csdnimg.cn/images/20210720083646.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)