Palindrome number C language

Source: Internet
Author: User

Title Description

If a number (the first not 0) from left to right read from right to left is the same, this number is called palindrome number, for example, 12521 is a palindrome number.
Given a positive integer, the number on each digit of it is arranged upside down to form a new number, and then add to the original number, if it is a palindrome stop, if not, then repeat the operation until and to the palindrome number. The given number itself is not a palindrome number.
For example: 87 are:
step1:87+78=165
step2:165+561=726
step3:726+627=1353
step4:1353+3531=4884
Write a program, input M (12<=m<=100), the output at least a few steps to get a palindrome number. If the palindrome number cannot be obtained within 8 steps (including 8 steps), the output is 0.

Input

Line 1th A positive integer l, which represents the number of groups of test data.
Next L line an integer m (12<=m<=100) per line, m itself is not a palindrome number;

Output

Output L line, line I for the input data of line i+1, output the minimum number of steps required, if the number of steps is greater than 8, then output 0.

Sample input
3128789
Sample output
140 Code

#include <stdio.h>
#include <string.h>
int reverse (int x)
{
int num=0;
while (x)
{
num=x%10+num*10;
x/=10;
}
return num;
}
int judge (int x)
{
if (X-reverse (x) ==0)
return 1;
Else
return 0;
}
int main ()
{
int l,m,cnt=0;
scanf ("%d", &l);
while (l--)
{
cnt=0;
scanf ("%d", &m);
while (cnt<=8)
{
if (!judge (m))
{
M=m+reverse (m);
cnt++;
}
Else
{
printf ("%d\n", CNT);
Break
}

}
if (cnt>8)
{
printf ("0\n");
}
}
return 0;
}

Palindrome number C language

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.