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!
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!
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.
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!
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
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)
And here's some functions from the C library which could help you resolve this problem: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 }
Now, show us what a 12-year old can do.
I hate real numbers.
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.
"What's up, Doc?"
"'Up' is a relative concept. It has no intrinsic value."
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.
Thanks everyone! Will post my code as soon as I get it done!
I totally apreiciate every single one in here
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); }
What is the value of x supposed to be? You never assign it a value.
%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; }
Last edited by noops; 07-24-2008 at 05:08 PM.
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; }
I still like my way better:
You could verify that the input was a number but it really doesn't have an effect on the program's outcome.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( "%s is%s palindromatic.\n", numbuff, palindromicity( numbuff ) ? "" : " NOT" ); return 0; }