Menu Close

C语言练习,检查一个整数是不是回文数

回文数是指正序(从左到右)读和倒序(从右到左)读都是一样的整数。

比如形如 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.
READ  C语言练习:计算标准偏差
除教程外,本网站大部分文章来自互联网,如果有内容冒犯到你,请联系我们删除!

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

Leave the field below empty!

Posted in C 决策和循环语句

Related Posts