C语言:判断一个数是否为回文数(回文数也是一个数字,数字的特点是正反序是同一个数字,如:12321,3443)

回文数的正反序是同一个数字,所以我们把这个数字的高低位交换,即1234->4321 ,然后再用新的得到的数字与以前的数字交换比较是否相等,即可以判断这个数是否为回文数。注意:负数不是回文数!

//判断一个数是否为回文数(回文数也是一个数字,数字的特点是正反序是同一个数字,如:12321,3443)
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

int if_palindrome_num(int n)
{
	assert(n > 0);
	int num = n;
	int res = 0;
	do
	{
		res = res * 10 + num % 10;
		num = num / 10;
	} while (num);
	if (res == n)
	{
		return 1;

	}
	return 0;
}

int main()
{
	int n = 12321;
	int temp = 0;
	temp = if_palindrome_num(n);
	if (temp == 0)
	{
		printf("The %d is not palindrome number.\n ",n);
	}
	else
	{
		printf("The %d is palindrome number.\n ",n);
	}
	system("pause");
	return 0;
}


猜你喜欢

转载自blog.csdn.net/gebushuaidanhenhuai/article/details/53131172
今日推荐