You can choose to manipulate the values as strings, reversing the string and
converting it to an integer to perform the addtion. You may need to write
your own conversion function as I'm not sure if C/C++ has a standard one for
differing bases (radix). You can then convert back to a string (in the same
base) and perform the manipulation again until you get something like normal
== reverse?
Another way is to make an array with an integer for each digit. That way you
can can just calculate the exponent for each possition according to it's
base, doing it forward and backward, and then break the result back down
into individual digits. i.e.
19 = 1 * 10^2 + 9 * 10^1
91 = 9 * 10^2 + 1 * 10^1
and
110 / 10^0 % 10 = 0
110 / 10^1 % 10 = 1
110 / 10^2 % 10 = 1
which are the individual digits of 110 etc...
It seems more complicated, but may be easier than writing your own
conversion from string to base 12 (or whatever) and back. I may do more work
later.
Jason.
PS: You may want to check my math as I just did it quickly, and it could be
done better.
PPS: '^' represents 'raised to the power of', but doesn't exist in C/C++,
and % is the modulus operator.
----- Original Message -----
From: "chromatonus 712" <chr...@ho...>
To: <dev...@li...>
Sent: Saturday, March 09, 2002 2:10 AM
Subject: [Dev-C++] palindrome base prgm
>
> hi, I am writing a prgm that takes a number (like 19), and its base, (10
in
> this case), and perform the following operation until a palindrome is
> obtained:
>
> add the reverse of 19
> 19+91 = 110
> add the reverse of 110
> 110 + 11 = 121....a palindrome,
> and the prgm should print out the 121.
>
> I think there are several caveats:
> bases can range from 10 to 16, so the A,B,C,D,E,F is tricky to handle as
> numbers, because the input would be like A32 for example.
> And how to reverse the number and check to see if it's a palindrome, and
> also doing addition in different bases. Any tip on these will be greatly
> appreciated!
>
>
> _________________________________________________________________
> Get your FREE download of MSN Explorer at
http://explorer.msn.com/intl.asp.
>
>
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|