Write your question here.
I'm almost finished, but can't complete my code to make it work. I want it to take user input of integers and output whether it is a palindrome or not. Then make it a palindrome and say how many integers are in the new number. I also need to make this work with more than a max of 5 digits, but this is what I've only been able to solve.
If you don't know what a palindrome is, it is a number that is the same forward as it is backwards. For example 1221 is a palindrome, but 12214 is not.
#include <iostream>
#include <math.h>
usingnamespace std;
int main()
{
unsignedlonglong originalnumber, tempnumber, tempnumber2, reversenumber = 0; // unsigned long long will allow for 10-digit inputs
cout << "Enter an integer (max 10 digits): ";
cin >> originalnumber;
tempnumber = originalnumber;
while(tempnumber != 0) // We'll keep dividing tempnumber until it's zero
{
int remainder = tempnumber % 10; // Get last digit of tempnumber
reversenumber = reversenumber * 10 + remainder; // Shift all digits in reversenumber to left, add last digit of tempnumber
tempnumber /= 10;
}
if(originalnumber == reversenumber)
{
cout << "Your number was a palindrome." << endl;
}
else
{
cout << "Your number was not a palindrome." << endl;
tempnumber = originalnumber;
int numberlength = 1; // Length of originalnumber, tempnumber, reversenumber
while(tempnumber /= 10) // Keep shifting digits right until tempnumber is 0
{
numberlength++;
}
tempnumber = originalnumber;
tempnumber2 = reversenumber;
int digitcut = 0; // How many digits to cut off numbers to arrive at palindrome
while(tempnumber != tempnumber2) // Keep cutting off digits until we have a palindrome
{
digitcut++;
tempnumber = tempnumber % int(pow(10, numberlength - digitcut)); // Cut first digit off originalnumber
tempnumber2 = reversenumber / int(pow(10, digitcut)); // Cut last digit off reversenumber
} // As an example, 12343 and 34321 would each be cut down to 343
tempnumber = originalnumber;
tempnumber2 = reversenumber;
tempnumber = tempnumber - (tempnumber % int(pow(10, numberlength - digitcut))); // Cut off all but highest *digitcut* digits
tempnumber = tempnumber * int(pow(10, digitcut)); // Shift tempnumber left *digitcut* places
unsignedlonglong palindrome = tempnumber + tempnumber2;
cout << "But this is: " << palindrome;
numberlength = 1;
while(palindrome /= 10) // Get length of final palindrome
{
numberlength++;
}
cout << " (" << numberlength << " digits)";
}
}
Hoo boy...that was harder than I thought. I'm very new to programming so it took me 2 hours. I hope this helped!
* There is a character in the 2D matrix, if column and row names are identical.
* There are two continuous diagonals from top/left edge to right/bottom edge, marked X and Z
Another representation for X and Z:
X:
12393--
--39321
Z:
12393----
----39321
What did I (almost) do there?
* Took a string and its reverse
* Filled a boolean 2D matrix with match/non-match
* Identified continuous diagonals.
* Took the longest diagonal
* Fill in the missing characters from the other string
You have the right idea logically for checking a palindrome, but you've overcomplicated it by leaving everything as a number. Since palindromes really depend on the string representation of a number rather than it's numeric value, convert the input number to a string first thing and then work with strings from there on out.
Note that there is a general trick here: the internal representation of your data does not have to correspond exactly to the external representation that you're given. Convert data to a form that's convenient for the program.
Returning to the problem, your code doesn't work for input 1232. It returns 1232321 instead of 12321.
Here is the approach that I took. I've removed most of the code in case you want to give this method a try.
// Return true if the suffix of s, starting at pos, is a palindrome.
bool isPal(const string &s, size_t pos = 0);
// Make "s" a palindrome by appending the minimum number of characters
// to the end.
void makePal(string &s)
{
// Go through the string until you find a suffix that's a
// palindrome.
size_t pos;
// CODE TO FIND THE LONGEST PALINDROMIC SUFFIX GOES HERE
// The suffix starting at pos is a palindrome.
// Append the prefix in reverse order
// CODE TO APPEND GOES HERE
}
int
main(int argc, char *argv[])
{
// This code takes strings as input instead of numbers.
string s;
while (cin >> s) {
makePal(s);
cout << s << ' ' << s.size() << '\n';
}
return 0;
}