Latest web development tutorials

C Exercise Examples 30-- palindrome

100 cases of classic C language 100 cases of classic C language

Title: a 5-digit, determine if it is a palindrome. That 12321 is a palindrome, the same bits and ten thousand, ten and one thousand identical.

Program analysis: learn decompose each digit.

Source Code:

//  Created by www.w3big.com on 15/11/9.
//  Copyright © 2015年 本教程. All rights reserved.
//

#include <stdio.h>

int main( )
{
    long ge,shi,qian,wan,x;
    printf("请输入 5 位数字:");
    scanf("%ld",&x);
    wan=x/10000;        /*分解出万位*/
    qian=x%10000/1000;  /*分解出千位*/
    shi=x%100/10;       /*分解出十位*/
    ge=x%10;            /*分解出个位*/
    if (ge==wan&&shi==qian) { /*个位等于万位并且十位等于千位*/
        printf("这是回文数\n");
    } else {
        printf("这不是回文数\n");
    }
}

The above example output is:

请输入 5 位数字:12321
这是回文数

请输入 5 位数字:12345
这不是回文数

100 cases of classic C language 100 cases of classic C language