I made this code that is supposed to increment a number till get the next palindrome number of of this inputted number.
The program take the number as string " cause it may be a very big digits number ( 0 < digits < 1000000 ) " ....
The Code
int main ()
{
string number = "1243";
int position = number.length()-1;
do
{
if (number[position] == '9')
{
//cout << "hereee";
number[position] = '0';
int n1 = (int)number[position-1] - '0';
n1++;
number[position-1] = n1 + '0';
nextPalindrome[position-1];
cout << number <<"hereee2"<< endl; // only to determine if i get in "if"
}
else
{
int n1 = (int)number[position] - '0';
n1++;
number[position] = n1 + '0';
cout << number <<"hereee1" << endl; // only to determine if i get in "else"
}
} while (isPalindrome(number) == false);
}
It's start to take the digit in the current position and increment it and return it as character again
The Problem
cout << number <<"hereee1" << endl;
this line show the number status while runing and it is like that :
12"6 hereee1
12"7 hereee1
12"8 hereee1
12"9 hereee1
12#0 hereee2
12#1 hereee1
while it must to be
1236 hereee1
1237 hereee1
1238 hereee1
1239 hereee1
1240 hereee2
1241 hereee1
i don't know where is the error .. can any one help
NOTE : "isPalindrome" is a function take a string as parameter and if the original string equal to its reverse, it returns true .. else return false
9
s, the next palindrome would be in the form10<.lot of 0s.>01
. If I have some time, I think I'll write it into the answer for my own pleasure - it is a good brain exercise. – ppeterka