Palindrome's Number

Hi, I was wondering what's wrong with my code, it seems logically correct if you follow through them.. My code is supposed to find palindromic numbers which are numbers that can be read backwards and forward and it will still be the same. For example, 1234321. Help would be greatly appreciated.


#include <iostream>
#include <cmath>

using namespace std;

int main()
{

int x = 100;
int y = 100;
int product;
while ( (1000 > x > 100) && (1000 > y > 100))
{
product = x*y;
if ( product > 100000)
{
int a[6];
int n = 0; //exponent, for example 10^n
int m; //digit of number
while (n < 6)
{
m = product / (10^n);
for ( int b=0; b < 6; b++)
{
a[b] = m;
}


}

if (a[0] == a[5] && a[1] == a[4] && a[2] == a[3])
cout << a[0] << a[1] << a[2] << a[3] << a[4] << a[5] << " is a palindromic number." << endl;


}

if (product < 100000)
{
int a[5];
int n = 0; //exponent, for example 10^n
int m; //digit of number
while (n < 6)
{
m = product / (10^n);
for ( int b=0; b < 6; b++)
{
a[b] = m;
}


}

if (a[0] == a[4] && a[1] == a[3])
cout << a[0] << a[1] << a[2] << a[3] << a[4] << " is a palindromic number." << endl;

}

x++;
y++;
}


return 0;





}
closed account (Dy7SLyTq)
look at the while loops condition. you cant have it that way
what do you mean? :O
closed account (Dy7SLyTq)
you cant have number > x > number. it has to be x < number && x < number
alright, i changed that but i still seem cant get an output.. :S
Is there any certain range where you'd like to extract the Palindrome's Numbers from?
Finally found it...good thing I bookmark topics I start

http://www.cplusplus.com/forum/general/86067/#msg468841

This was my first ever post on cplusplus forum and it is a palindrome number checker. The parameters take the number you are trying to determine if it is palindrome and it takes the length of the number(log10(number)) and it returns true if the number is palindrome and false if not. Of course now that I look at it, there are many optimizations that can be made to it. But it runs pretty fast because it is able to find the 1 billionth palindrome value starting from 1 in about .03 ms. This was of course due to some optimizations used in the main code.

Hint in terms of optimization:
numbers divisible by 10 are not palindrome. There is no such thing as 010 in base 10 numbers
use the "<>" button to insert code in format into the forum

1
2
3
void foo(){ 
   solve everything
}
here is the code for the function:

bool isPal (int number) {

if (number == 0)
return true;
else
int length = log (number);

if (number%10)
return false;

else if ( length ==1)
return true;

else if (length == 2) {
if (number%11 == 0)
return true;
else
return false;
}

else
if (number%10 == number/pow(10,length)) {
isPal (number - (number/pow(10, length))*pow(10, length) - number%10)
}
else return false;
}
}
Last edited on
Yes, thank you, but here's the thing, someone could just post their code and have me just copy and paste it. I want to know what's wrong with my code. It seems that's what you guys are somewhat doing, putting the numbers in an array and comparing the values. Which is the same approach I tried. But nothing shows up. It's just a blank commant prompt.
sorry, the function doesn't work. POW should be operated on float or double, not with integers.
I now have a question: how do we raise 10 to a certain power and keep it as an integer??
what do you mean by that? what i'm trying to do is set up an array of x elements so that each number of the product fits into a spot in the array. or does that not work?
what do you need the product for? all you need is generate numbers within a certain range then check if each number is a Palindrome.

This would be easier, compare the reverse of the a number and if it equals the number then it is Palindrome.
http://www.cplusplus.com/forum/beginner/106760/2/

Last edited on
i actually took a break from coding due to midterms and the fact that i am too far behind with school. i am finally done with finals. so i was wondering what's wrong with my method? am i over complicating things?
closed account (Dy7SLyTq)
probably. whats the problem again?
Your function will always return false if a number does not end with 0 due to the statements

if (number%10)
return false;

For example 11 % 10 != 0 so the function returns false.

By the way function log() computes the base-e (natural) logarithm. You should use function log10().
Last edited on
Topic archived. No new replies allowed.