Using while to determine whether the number of the input is a simple realization of _c language

Source: Internet
Author: User

Copy Code code as follows:

/*
Name: Use while to determine if the number entered is a palindrome
Copyright:by. Don't know the Internet
Author:yangbin
DATE:2014 Year February 18 04:29:07
Description: Use while to determine whether the number of user input palindrome, is a palindrome number return yes! otherwise no!
*/
# include <stdio.h>
int main (void)
{
int m,val,sum = 0;
printf ("Please enter a palindrome number, if it is a palindrome number return yes, otherwise return no:");
scanf ("%d", &val);
m = val;
while (m)
{
sum = sum*10+m%10;
M/= 10;
}
if (sum = = val)
printf ("yes\n");
Else
printf ("no\n");

}
/*
----------------------
This code is written by C-free 5.0 and outputs debug results
-----Output-----
Please enter a palindrome number, if it is a palindrome number return Yes, otherwise return to no:1221
YES
------Summary------
When you can't write this code, see how others write, and then understand the meaning of the Code;
This code is primarily the calculation of a process for a while.
*/

Process analysis of a while statement
Suppose the user input number 121, judge whether Palindrome number, M non 0 is true, otherwise false, for true output Yes, false output no;

1--->sum = sum*10+m%10 (sum = 0*10+121%10)//sum=0+1,sum=1
M/= ten (M=M/10)//m=121/10,m=12
2--->sum = sum*10+m%10 (sum=1*10+12%10)//sum=10+2,sum=12
M/10 =10 (M=M/10)//m=12/10,m=1
3--->sum = sum*10+m%10 (sum=12*10+1%10)//sum=120+1,sum=121
M/10 =10 (M=M/10)//m=1/10,m=0

m = 0 is false, output no

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.