/* ************************************************************************** * Program name : 016_Palindrome_number (Version 1.00) * * Author : Duck Wong * * Language : C / C++ * * Compiler : Boodshed Dec-C++ compiler Ver 3.95 * * Computer : PII350 * * O/S : Windows 98 * ************************************************************************** * Version 1.00 : 2000/06/09 - first version * ************************************************************************** * Description : (a) Input an integer number * * (b) Is that a palindrome number ? * * (e) Try again ? * ************************************************************************** */ #include #include #include // Note (1) #include // Note (2) int main() { // part 1 : declaration int Number, Size, Div, Index, TempNum, Comp_1, Comp_2; char Pali, again; cout << "The palindrome is a number or a text phrase that reads the same\n" << "backwards as forwards. For example : 12321, 55555 and 45554.\n"; do { // part 2 : Repeat asking the valus of base amount until base amount > 0 do { cout << "\nPlease input an integer number : "; cin >> Number; if (Number<=0) // Note (3) { cout << "\nThe number must be greater than zero(o)!"; continue; }; // part 3 : Find the length of the inpuuted number Div = 1; Size = 0; for (Index=1; Index<=10; Index++) // Note (4) { Div *= 10; Size += 1; if ((Number / Div)==0) break; } cout << "\nThe inputted number is : " << Number; cout << "\nNo. of digit is : " << Size << endl; if ((Size % 2)==0 || Size==1) // Note (5) { cout << "\nThis program need an integer " << "with 3,5,7 or 9 digits. \nPlease try again!\n"; continue; }; } while ((Number<=0) || (Size % 2)==0 || (Size == 1)); // part 4 : Is that a palindrome number ? Pali = 'Y'; TempNum = Number; // Note (6) Index = 1; // Note (7) cout << "\nRound Number First Last Result\n" << "-------------------------------------------------------------\n"; do { Comp_1 = TempNum % 10; // Note (8) Comp_2 = TempNum /(pow(10,Size-1)); // Note (9) cout << Index << "\t" << setw(9) << TempNum << "\t" << Comp_1 << "\t" << Comp_2; if (Comp_1 != Comp_2) // Note (10) { cout << "\tError found\n"; Pali = 'N'; break; } else { TempNum = (TempNum-(Comp_2*pow(10,Size-1)))/10; // Note (11) Size -= 2; Index++; cout << "\tPass, " << Size << " digit(s) left\n"; }; } while (Size!=1); if (Pali=='Y') cout << "\a\n" <> again; cout << "\n"; } while (again=='Y' || again=='y'); cout << "\n" << endl; system("PAUSE"); return 0; } /* Notes (1) Use "#include " because this program needs the function "pow". (2) Use "#include " because this program needs the function "setw". (3) This program does not support negative number. (4) Check the no. of digit inputted: Example : 12321 Index Div Size Number/Div 1 10 1 1232 2 100 2 123 3 1000 3 12 4 10000 4 1 5 100000 5 0 (stop checking when Number/Div is 0) Therefore the number 12321 contains 5 digits. (5) When ((Size % 2)==0 || Size==1), that means the no. of digits is an even number or that number contains one digit only. However this program does not support negative number. (6) Use the variable "TempNum" to represent the value of "Number" in the loop of checking. (7) Use "Index" to keeps the number of round. (8) Use "TempNum % 10" to find the last digit then stored in "Comp_1". (9) Use "TempNum /(pow(10,Size-1))" to find the first digit then stored in "Comp_2". (10) When (Comp_1 != Comp_2), that means the inputted number is not a palindrome number. (11) Find the value of TempNum for the next round: Example : 1234221 Round TempNum first(comp_2) last(comp_1) Reault ------------------------------------------------------------------------ 1 1234221 1 1 Pass, 5 digit(s) left 2 23422 2 2 Pass, 3 digit(s) left 3 342 3 2 Error found When TempNum is 23422 and Size is 5, then pow(10,Size-1) == 10000 Comp_2*pow(10,Size-1) == 20000 TempNum-(Comp_2*pow(10,Size-1)) == 3422 (TempNum-(Comp_2*pow(10,Size-1)))/10 == 342 TempNum == 342 (for the next round) */