Adding numbers to get a Palindrome

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();
}
I suggest you to convert lines 17-21 into a function.
The problem is that you don't reset `d' which holds the reversed number on the previou step.
> ... and keep on repeating this process until a palindrome is obtained.

Or an unsigned integer overflow is encountered.

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
37
38
#include <iostream>
#include <limits>

unsigned long long reverse( unsigned long long number )
{
    unsigned long long rev = 0 ;
    for( ; number != 0 ; number /= 10 ) rev = rev*10 + number%10 ;
    return rev ;
}

bool palindrome( unsigned long number ) { return number == reverse(number) ; }

int main()
{
    constexpr unsigned int MAX = std::numeric_limits<unsigned int>::max() ;
    static_assert( std::numeric_limits<unsigned long long>::max() > ( MAX * 10ULL ), "" ) ;

    for( unsigned int n : { 28, 10507800, 87, 789432, 123456, 123321, 541296, 888899999 } )
    {
        unsigned long long number = n ;
        int n_iterations = 0 ;

        for( ; !palindrome(number) ; ++n_iterations )
        {
           std::cout << number << " => " ;

           number += reverse(number) ;

           if( number > MAX )
           {
               std::cerr << "unsigned integer overflow\n" ;
               return 1 ;
           }
        }

        std::cout << number << "    #iterations:" << n_iterations << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/3a2ca79a510662f2
Topic archived. No new replies allowed.