A smart way to invert a number

/This is my homework today to judge a number whether it's a palindrome./
/Most of us use array to solve the problem, but I find a way do not need an array./

include <stdio.h>

int main(int argc, const char * argv[])
{
int x,m,n=0;
printf("Enter a number:");
scanf("%d",&x);
for(int i=1;i<=x;i * =10)
{
m=x%(i * 10)/i;
n=m+n*10;
}
if (n==x)
{
printf("%d is a palindrome\n",x);
}
else
{
printf("%d is not a palindrome\n",x);
}
return 0;
}

posted @ 2024-02-23 14:32  我要成为(c语言)高手  阅读(17)  评论(0)    收藏  举报