1. ## Palindrome in C

Hello!
Was just wondering if there's a C program which can take from the user a number and it checks if its a palindrome or not...I know that we %10
right ?
May you show it to me!

2. Yeah, we could, but that kind of defeats the purpose, doesn't it? You learn best if you actually write the code out, which could probably be done within one day. If you're really stuck, you could find it on google probably.

3. I could show you a palindrome program but I need to write a paper on the Boston tea party. I know it involves tea. And Boston. Could you write it for me?

Thanks!

4. Well..if you get into details about what you want me to write..would be my pleasure
as for the palindrome I will do the Macgyver said..I will try first and when I give up I will come in here

5. Here's an advice, DaniiChris; it's easier to check if a string is a palindrome than if a number is. So here's an idea of how your program should looks like (I'm giving you the design, you'll have to implement the algorithms)

Code:
```int isPalindromeString(const char *s)
{
// This function should return 1 if the string is a palindrome, else 0
}

int isPalindromeInt(int n)
{
// This function should return 1 if the integer (or it's absolute value) is a palindrome, else 0
// Note that this function should call the isPalindromeString function. It just need to transform the int into a string
}```
And here's some functions from the C library which could help you resolve this problem:

Now, show us what a 12-year old can do.

6. My idea was to place the input into an array (easy to do if you can use getchar to collect input). Then just iterate through the array.

7. Originally Posted by noops
My idea was to place the input into an array (easy to do if you can use getchar to collect input). Then just iterate through the array.
Then just iterate through the array and exit? Wow, you should get a cookie for some specific instructions.

Using getchar to complicate things would be .. superfluous. I'd just use fgets and then reverse the string, store it elsewhere, and compare it to the original string. You can also just iterate through the array using two indexers - one starting at the start, and the other at the end, and compare the characters in both positions.

8. If a number has k digits, (which you should know if you've converted it to a string) then compare every digit i to k - i. If they're all equal, you have a palindrome. Not hard.

9. Thanks everyone! Will post my code as soon as I get it done!
I totally apreiciate every single one in here

10. I gave it a try! and here it is
Thanks to everyone
Code:
```#include <stdio.h>
int main()
{
int x,num,p,rev;
printf("Enter your desired number you want to check");
scanf("%i",&num);

while(num > 0)
{
p = num % 10;
rev = num*10 + p;
rev = num / 10;
rev = x;
}

if (x == rev)
printf("Yes,Its a Palindrome");
else
printf("No,It isn't a Paindrome");
return(0);
}```

11. What is the value of x supposed to be? You never assign it a value.

12. &#37;10 will only give you the last digit. Also, you are never updating the num variable in your while loop so it will probably always be an infinite loop. Here's how you do it using math:

Code:
```while (num >= 10)
{
for (i = 10; i * 10 < num; i *= 10);
if (num / i != num % 10)
palindrome = 0;
num = (num - (num / i) * i) / 10;
}```

13. I came up with this in about 5 minutes. Of course it doesn't work, because I intentionally broke it. May you fix it?

(Code highlighting courtesy of dwks' Codeform!)
Code:
```#include <stdio.h>
#include <stdlib.h>

int is_palindrome(int number) {
int n = abs(number);
int r = 0;

while (n > 0) {
r = r + n%10;
n /= 10;
}

return r == number;
}

int main(int argc, char **argv) {
int num;

if (argc > 1)
sscanf(argv[1], "%d", &num);
else {
printf("Enter a number: ");
fflush(stdout);
scanf("%d", &num);
}

printf("%s", is_palindrome(num) ? "YES" : "NO");
putchar('\n');
return 0;
}```

14. I still like my way better:

Code:
```#include <stdio.h>
#include <string.h>

int palindromicity ( const char * numbuff )
{
size_t i, len;
for( i = 0, len = strlen( numbuff ); i < len; i++ ) {
if( numbuff[i] != numbuff[len - 1 - i] ) return 0;
}

return 1;
}

int main ( void )
{
char numbuff[32];
printf( "Please enter a integer:\n" );
fgets( numbuff, sizeof numbuff, stdin );
numbuff[strcspn( numbuff, "\n" )] = '\0';

printf( "&#37;s is%s palindromatic.\n", numbuff,
palindromicity( numbuff ) ? "" : " NOT" );

return 0;
}```
You could verify that the input was a number but it really doesn't have an effect on the program's outcome.