Judge whether a number is a palindrome

Source: Internet
Author: User

Enter a numerical value to determine whether the palindrome number (palindrome number is a like 12321, 123321 such "symmetry" number)

Earlier on the Internet to see some of the number of palindrome to judge some of the procedures, some big guy to judge palindrome number of methods divided into several categories, today I wrote just a very elementary very simple to judge the number of int type palindrome data program.

#define _crt_secure_no_warnings 1

 

#include <stdio.h>

#include <stdlib.h>

#include < math.h>

int main ()

{

int n = 0;

int count = 1;

int m = 0;

int i = 0;

int k = 0;

int flag = 0;

int a[100];

printf ("Please enter a number \ n");

scanf ("%d", &n);

m = n;

while (M > 9)

{

a[i] = m%;

m = M/10;

i++;

 

}

A[i] = m;

K = i;

Count = i+1;

for (i = 0; i < COUNT/2 i++)

{

if (a[i)!= a[k])

{

flag = 1;

break;

k--;

}

if (flag = = 1)

{

printf ("This number is not a palindrome number \ n");

}

else

{

printf ("This number is a palindrome number \ n");

}

System ("pause");

return 0;

}

First of all, briefly introduce my thought of judging palindrome number, I split the input int, put each of his digits into an integer array, then compares the first and last digits of the array, then compares the second and the second to the bottom. In turn, if you find different words do not need to continue to compare the rest of the data, directly out of the loop, if until the end of the cycle is not out of the loop that means that the number before and after each number is symmetrical, then he is what we call the palindrome number.

while (M > 9)

{

A[i] = m% 10;

m = M/10;

i++;

}

A[i] = m;

Every bit of M is split apart, first modulo 10 that gets the single-digit number of M, it then saves it to the corresponding array a, and then divides m by 10, which means the last one is removed, because the int type is not stored after the decimal point, and continues to be stored after modulo 10. But when the M is left with only one digit, while the loop jumps out, the first bit of M is not yet deposited into the array, so we need to write a a[i] = M statement.

And now I'm the number of digits minus 1 because the array is starting from 0 so your number m should have count = i+1; bit

for (i = 0; i < COUNT/2; i++)

{

if (A[i]!= a[k])

{

flag = 1;

Break

}

k--;

}

Then use the For loop to start comparing the first and last in your array if they are not equal then jump straight out, if equal, then let i++,k--, continue to contrast.

if (flag = 1)

{

printf ("This number is not palindrome number \ n");

}

Else

{

printf ("This number is palindrome number \ n");

}

At the end of the flag to judge, if flag equals 1 that means he jumped out of the loop to explain the number of digits is not equal, so it is not a palindrome number, otherwise there is no jump out of the cycle that the number is what we call the palindrome number.

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.