Hi,
As you might know that almost all numbers can be converted to palindromes if we add their reversed form to the original number, and repeat this process.
I was trying to make a program that will do this and give the user output as what is the final palindrome number and how many iterations took place to get there.
My program works fine if the original input given in itself was a palindrome however it fails when I give it a non palindrome number initially like 10.
The process I want it to do when a number like 10 goes in it is:
10
reversed number = 01 or simply 1
add this to original number
10+1=11
check if 11 is palindrome, if yes show the palindrome and number of iterations taken place. else reverse this again, add it to number received and check again and keep on repeating this process until a palindrome is obtained.
My code can be found below. Please help
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
#include <iostream>
#include "conio.h"
using namespace std;
void main(){
unsigned long int number, c, d=0, flag = 1, i = 1, z ;
cout << "Enter a positive number" << endl;
cin >> number;
cout << endl;
for (i;i>0;i++){
z = number;
do{
c = z % 10;
d = d * 10 + c;
z /= 10;
} while (z != 0);
if (d == number)
{
cout << "The palindrome number obtained is: " << d << endl << "Number of iterations taken place = " << flag;
break;
}
else
{
number+= d;
flag++;
}
}
_getch();
}
|