Latest web development tutorials

C 練習實例30 – 回文數

C 語言經典100例 C語言經典100例

題目:一個5位數,判斷它是不是回文數。 即12321是回文數,個位與萬位相同,十位與千位相同。

程序分析:學會分解出每一位數。

程序源代碼:

//  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");
    }
}

以上實例輸出結果為:

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

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

C 語言經典100例 C語言經典100例