C Exercise Example 30 - Palindrome Number

100 Classic C Examples

Title:A 5-digit number, determine whether it is a palindrome. That is, 12321 is a palindrome, the units digit is the same as the ten-thousands digit, and the tens digit is the same as the thousands digit.

Program analysis:Learn to decompose each digit.

Program source code:

Example

// Created by www.example.com on 15/11/9. // Copyright © 2015 Example. All rights reserved. // #include <stdio.h> int main( ) { long ge,shi,qian,wan,x; printf("Please enter a 5-digit number:"); scanf("%ld",&x); wan=x/10000; /*Extract the ten-thousands digit*/ qian=x%10000/1000; /*Extract the thousands digit*/ shi=x%100/10; /*Extract the tens digit*/ ge=x%10; /*Extract the ones digit*/ if (ge==wan&&shi==qian) { /*The ones digit equals the ten-thousands digit and the tens digit equals the thousands digit*/ printf("This is a palindrome number\n"); } else { printf("This is not a palindrome number\n"); } }

The output result of the above example is:

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

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

100 Classic C Examples

other extensions