C language to determine whether a number is a palindrome number

Source: Internet
Author: User
Tags printf

Note: palindrome number is the number along and reverse is the same number!

Read Haobin Teacher's C language video, although still only read more than 80, but still have some experience, programming should develop good programming style, at least so far write these small programs should have the following such a process:

1, process (know how the program is running in the same order)

2, function (understanding the role of the program)

3, test number (I personally understand the test process, as a computer to carry out the program)

/*

April 20, 2012 10:36:23

Judge whether a number is a palindrome number

*/

#include <stdio.h>

int main (void)

{

int Val; Storing data to be judged

int m;

int sum = 0;

printf ("Please enter the number you need to judge: \ n");

scanf ("%d", &val);

m = val;

/* This piece is the core code block:

From the number of input from the sum of 10, hundreds ... In order to get the inverse number, we need to *10 and add the next one as a single-digit

Sum is updated every time, the M value is removed and the last one is updated

*/

while (m)

{

sum = sum*10 + m%10;

M/= 10;

}

if (sum = = val)

printf ("yes!\n");

Else

printf ("no!\n");

return 0;

}

Trial process (pure physical activity)

/*

The process of judging whether it is a palindrome number

Just test the core block here.

*/

1234

1-> m = val = 1234; sum = 0 + 1234%10 = 4; m = 1234/10 = 123;

2-> m = 123; sum = 40 + 123%10 = 40+3 = 43; m = 123/10 = 12;

3-> m = 12; sum = 430 + 12%10 = 432; m = 12/10 = 1;

4-> m = 1; sum = 4320 + 1%10 = 4321; m = 1/10 = 0;

False

12321

1-> m = val = 12321; sum = 0 + 12321%10 = 1; m = 12321/10 = 1232;

2-> m = 1232; sum = 10 + 1232%10 = 12; m = 1232/10 = 123;

3-> m = 123; sum = 120 + 123%10 = 123; m = 123/10 = 12;

4-> m = 12; sum = 1230 + 12%10 = 1232; m = 12/10 = 1;

5-> m = 1; sum = 12320 + 1%10 = 12321; m = 1/10 = 0;

False

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/C/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.