Easy Tutorial
❮ C Standard Library Setjmp H C Examples Standard Deviation ❯

C Exercise Example 30 - Palindrome Number

C Language Classic 100 Examples

Topic: A 5-digit number, determine if it is a palindrome number. For example, 12321 is a palindrome number, where the ones place is the same as the ten-thousands place, and the tens place is the same as the thousands place.

Program Analysis: Learn to decompose each digit.

Program Source Code:

Example

//  Created by www.tutorialpro.org on 15/11/9.
//  Copyright © 2015 tutorialpro.org. 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;        /* Decompose the ten-thousands place */
    qian = x % 10000 / 1000;  /* Decompose the thousands place */
    shi = x % 100 / 10;       /* Decompose the tens place */
    ge = x % 10;            /* Decompose the ones place */
    if (ge == wan && shi == qian) { /* Ones place equals ten-thousands place and tens place equals thousands place */
        printf("This is a palindrome number\n");
    } else {
        printf("This is not a palindrome number\n");
    }
}

The above example output is:

Please enter a 5-digit number: 12321
This is a palindrome number

Please enter a 5-digit number: 12345
This is not a palindrome number

C Language Classic 100 Examples

❮ C Standard Library Setjmp H C Examples Standard Deviation ❯