I am writing a program which asks the user for input, how many digits to use, on 2 numbers which will multiply themselves. A loop will multiply all combinations of that number digits and determine the largest palindrome. I know how to do palidnromes with strings, but don't know if there is an easier way with integers. This is what I have to find the palindromes til now. Can someone help?

``````void multiplyNums(int *number)
{
char *stemp;
stemp = new char[80];
itoa(*number, *stemp, 10);
for(int i = 0; i < stemp.size(); i++)
if(isalnum(stemp[i]))
s += tolower(stemp[i]);
if(isPalin(s))
cout << stemp << " is a palindrome\n";
else
cout << stemp << " is not a palindrome\n";

}
bool isPalin(string& s)
{
for(int i=0; i<s.size(); i++)
if(tolower(s[i]) != tolower(s[s.size()-1-i]))
return 0;
return 1;
}``````
4
Contributors
13
Replies
17
Views
7 Years
Discussion Span
Last Post by Nandomo

you can use 'number % 10' to find out the last digit of number.
and you can loop finding out how many digits are there in the number.
then just keep using '%' and '/' to find out the first and last digit of the number and compare, and split the first digit and the last digit out using '%' and '/', until 0 left there.

and itoa is not standard
try using stringstream of C style sprintf

I understand, so you would use %100, %1000 and so on, if the number has many digits?

Not letting me edit last post.... I implemented what I was told and its not working for some reason. Any help?

``````#include <iostream>
#include <cstdlib>
#include <windows.h>
#include <iomanip>
#include <string>
#include <cctype>

using namespace std;

int multiplyNums(int *);

int main()
{
int *ptrChoice;
ptrChoice = new (int);

int *ptrAns;
ptrAns = new (int);

cout << setfill(' ') << setw(31) << " " << "Palindrome Numbers" << endl;
cout << setfill('-') << setw(80) << "-" << endl;

cout << "Please choice an option to continue (Use 1-6):" << endl;
endl(cout);
cout << "\t 1. Two Digits. " << endl;
cout << "\t 2. Three Digits. " << endl;
cout << "\t 3. Four Digits. " << endl;
cout << "\t 4. Five Digits. " << endl;
cout << "\t 5. Six Digits. " << endl;
cout << "\t 6. Exit Program " << endl;
endl(cout);
cout << "Option: ";
while (!(cin >> *ptrChoice))
{
char ch;
cin.clear();
cout << "Please enter only numbers: ";
endl(cout);
while (cin.get ( ch ) && ch != '\n');
}
switch (*ptrChoice)
{
// Send to change2Negative to convert values
case 1: *ptrAns = multiplyNums(ptrChoice);
// Send to makeLighter to convert values
case 2: *ptrAns = multiplyNums(ptrChoice);
// Send to makeDarker to convert values
case 3: *ptrAns = multiplyNums(ptrChoice);

case 4: *ptrAns = multiplyNums(ptrChoice);

case 5: *ptrAns = multiplyNums(ptrChoice);
// Exit
case 6: exit(0);
break;
default: cout << "Please input a valid number." << endl << endl;
}

endl(cout);
cout << "Largest Palindrome is " << *ptrAns << "." << endl;

delete ptrChoice;
system("PAUSE");
return EXIT_SUCCESS;
}

int multiplyNums(int *number)
{
if(*number == 1)
{
int *palin = NULL;
palin = new (int);
int *num = NULL;
num = new (int);
for(int i = 10; i < 100; i++)
{
for(int j = 10; j < 100; j++)
{
*num = i * j;

int temp = *num;
int sum = 0;
while (temp)
{
sum *= 10;
sum += temp % 10;
temp /= 10;
}

if (sum == *num);
*palin = sum;

}
}
delete palin;
delete num;
return *palin;
}

}``````

>> I know how to do palidnromes with strings, but don't know if there is an easier way with integers

Then covert to a string and do the palindrome check.

I don't see a `bool IsPalindrome(int)` function anywhere. Don't you need one?

One way or another, you need to isolate the first and last digits and compare them. I don't see you doing that anywhere.

Any why are you creating all of these pointers to integers with the new command instead of simply declaring a regular old integer and using that?

I'm not sure what your algorithm is. Are you calculating all sums, then checking whether each is a palindrome? Or are you using some math and checking? Six digits times six digits is a lot of possibilities so brute force is going to be problematic.

I am supposed to be using dynamic memory allocation. I prefer not to use strings and keep the numbers as int's. I am using this to check if palindrome.

``````if (sum == *num);
*palin = sum;``````

Since the following reverses the number given to it.

``````int temp = *num;
int sum = 0;
while (temp)
{
sum *= 10;
sum += temp % 10;
temp /= 10;``````

We won't talk about 6 digits yet, since I am doing 2 digits first, once I get the coding for 2 digits, since is just a minor conversion in code. I will eventually send the check if Palindrome to another function, but want it to all work from that function first. Any ideas?

See if deleting the semicolon in line 93 helps.

I modified a bit. Still not returning to main. Like it is supposed to.

``````#include <iostream>
#include <cstdlib>
#include <windows.h>
#include <iomanip>
#include <string>
#include <cctype>

using namespace std;

int multiplyNums(int *);
bool checkIfPalindrome(int *, int);

int main()
{
int *ptrChoice;
ptrChoice = new (int);

int *ptrAns;
ptrAns = new (int);

cout << setfill(' ') << setw(31) << " " << "Palindrome Numbers" << endl;
cout << setfill('-') << setw(80) << "-" << endl;

cout << "Please choice an option to continue (Use 1-6):" << endl;
endl(cout);
cout << "\t 1. Two Digits. " << endl;
cout << "\t 2. Three Digits. " << endl;
cout << "\t 3. Four Digits. " << endl;
cout << "\t 4. Five Digits. " << endl;
cout << "\t 5. Six Digits. " << endl;
cout << "\t 6. Exit Program " << endl;
endl(cout);
cout << "Option: ";
while (!(cin >> *ptrChoice))
{
char ch;
cin.clear();
cout << "Please enter only numbers: ";
endl(cout);
while (cin.get ( ch ) && ch != '\n');
}
switch (*ptrChoice)
{
// Send to change2Negative to convert values
case 1: *ptrAns = multiplyNums(ptrChoice);
// Send to makeLighter to convert values
case 2: *ptrAns = multiplyNums(ptrChoice);
// Send to makeDarker to convert values
case 3: *ptrAns = multiplyNums(ptrChoice);

case 4: *ptrAns = multiplyNums(ptrChoice);

case 5: *ptrAns = multiplyNums(ptrChoice);
// Exit
case 6: exit(0);
break;
default: cout << "Please input a valid number." << endl << endl;
}

endl(cout);
cout << "Largest Palindrome is " << *ptrAns << "." << endl;

delete ptrChoice;
system("PAUSE");
return EXIT_SUCCESS;
}

int multiplyNums(int *number)
{
if(*number == 1)
{
int temporary;

int *num = NULL;
num = new (int);
for(int i = 10; i < 100; i++)
{
for(int j = 10; j < 100; j++)
{
*num = i * j;

int temp = *num;
int rev = 0;
while (temp)
{
rev *= 10;
rev += temp % 10;
temp /= 10;
}

if(checkIfPalindrome(num, rev))
temporary = *num;
}
}

//cout << temporary << endl;

delete num;
return temporary;
}

}
bool checkIfPalindrome(int *number, int reverse)
{
if (reverse == *number)
return true;
return false;

}``````

You need to stick some debugging output in there and trace what's happening. You could be in an infinite loop, you could not be calling the right function, not passing the right parameter, or you could be not waiting long enough for the function to complete.

As you know,
1) To check if the integer is a palindrome, you need to look at individual digits.
2) And you already know how to check if a string is a palindrome.
3) Also, isn't a string just a character array?

Armed with this info, break the integer into an array of digits (ints) using that % / stuff:

``````i=0;
while num < 0
digits(i++) = num % 10
num /= 10``````

Now check if the digits array is a palindrome.

Yes, the digits array will be in reversed order, but when checking for a palindrome it doesn't matter.

I have debugged to fix some of my allocation problems. Can you give me a little tutorial on how to use the debugging tool?

The best debugging tool you have is `cout` . Display the variables at various spots in your program to see what's happening at key statements.

Thanks for the colaboration of all, I got the program running. Want me to post it up? ^.^