How to write a palindrome number using a for loop in the C language?

A palindrome number refers to a number that reads the same from left to right as it does from right to left, such as 121 or 1221.

Below is an example code written using a for loop to determine if a number is a palindrome.

#include <stdio.h>

int main() {
    int num, reversedNum = 0, originalNum, remainder;

    printf("请输入一个数字: ");
    scanf("%d", &num);

    originalNum = num;

    // 将数字反转
    for (; num != 0; num /= 10) {
        remainder = num % 10;
        reversedNum = reversedNum * 10 + remainder;
    }

    // 判断是否为回文数
    if (originalNum == reversedNum) {
        printf("%d 是一个回文数。\n", originalNum);
    } else {
        printf("%d 不是一个回文数。\n", originalNum);
    }

    return 0;
}

After the user inputs a number, the program will reverse the number and compare it with the original number to determine if it is a palindrome.

bannerAds