回文数是指正序(从左到右)读和倒序(从右到左)读都是一样的整数。
比如形如 121,1221,13531 的数字都是回文数,但 -121,10,25 等等都不是回文数。
#include <stdio.h> int main() { int n, reversed = 0, remainder, original; printf("Enter an integer: "); scanf("%d", &n); original = n; // reversed integer is stored in reversed variable while (n != 0) { remainder = n % 10; reversed = reversed * 10 + remainder; n /= 10; } // palindrome if orignal and reversed are equal if (original == reversed) printf("%d is a palindrome.", original); else printf("%d is not a palindrome.", original); return 0; }
Enter an integer: 1001
1001 is a palindrome.
除教程外,本网站大部分文章来自互联网,如果有内容冒犯到你,请联系我们删除!