|
From: Samuel K. <big...@ya...> - 2002-03-12 04:06:16
|
This will only work for base 10 and there are surely many ways of doing the
palindrome that are a lot easier to understand and more economical on
computer processor time but I thought you all might want to take a look.
(don't be to harsh on me, I haven't been programming very long)
#include <iostream>
int main(){
int inputNumber,
firstSentinal = 10, //Counts up Digits
//varible to "flip" inputNumber
secondSentinal = 1,
editNumber = 0;
cout << "Please input number:";
cin >> inputNumber;
while (inputNumber / firstSentinal >= 10){
firstSentinal *= 10;
}
//Keeps secondSentinal from overflowing
const int Guard = firstSentinal * 10;
while ( secondSentinal != Guard ){
editNumber += (inputNumber / firstSentinal) * secondSentinal;
//Does work of flipping
inputNumber %= firstSentinal;
firstSentinal /= 10;
secondSentinal *= 10;
}
cout << endl << editNumber <<"\n\n";
cin.ignore(256, '\n');
cout << "Press ENTER to continue..." << endl;
cin.get();
return 0;
}
As I said this will only work base ten. To use it for the palindrome make
another const copy of inputNumber and then if editNumber = = inputNumber you
know that it is a palindrome. This might be portable to hex, but I'm not
sure.
_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com
|