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

C Language Examples - Checking for a Palindrome Number

C Language Examples - Checking for a Palindrome Number

C Language Examples C Language Examples

Determine whether a number is a palindrome.

Let n be any natural number. If reversing the digits of n yields a natural number n1 equal to n, then n is called a palindrome number. For example, if n=1234321, then n is a palindrome; but if n=1234567, then n is not a palindrome

Example

#include

<

stdio.h

int

main

(

)

{

int

n

,

reversedInteger

=

0

,

remainder

,

originalInteger

;

printf

(

"

Enter an integer:

"

)

;

scanf

(

"

%d

"

, &

n

)

;

originalInteger

=

n

;

// Reverse

while

(

n

!=

0

)

{

remainder

=

n

%

10

;

reversedInteger

=

reversedInteger

10

remainder

;

n

/=

10

;

}

// Check

if

(

originalInteger

==

reversedInteger

)

printf

(

"

%d is a palindrome number.

"

,

originalInteger

)

;

else

printf

(

"

%d is not a palindrome number.

"

,

originalInteger

)

;

return

0

;

}

Running result:



输入一个整数: 12321

12321 是回文数

C Language Examples C Language Examples