Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Exercise 30 - Palindrome Number

C Exercise 30 - Palindrome Number

100 Classic C Language Examples 100 Classic C Language Examples

**Problem:** Given a 5-digit number, determine whether it is a palindrome. For example, 12321 is a palindrome: the ones digit equals the ten-thousands digit, and the tens digit equals the thousands digit.

**Program Analysis:** Learn to break down each digit.

Program source code:

Example

// Created by www.runoob.com on 15/11/9.

// Copyright (c) 2015 Runoob Tutorial. 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

)

{

/*

Ones digit equals ten-thousands digit and tens digit equals thousands digit

*/

printf

(

"

This is a palindrome

\

n

"

)

;

}

else

{

printf

(

"

This is not a palindrome

\

n

"

)

;

}

}

The output of the above example is:



请输入 5 位数字:12321

这是回文数



请输入 5 位数字:12345

这不是回文数

100 Classic C Language Examples 100 Classic C Language Examples