V22.0002 - ASSIGNMENT 6
Write a program that produces palindromic numbers: numbers that are
the same forward as backward.
Use the following algorithm:
- Start by taking a number.
- Find the number formed by reversing the order of the digits in
the number (we shall call this the reverse number).
- If the original number is not a palindrome:
- Add the reverse number to the number.
- Go back to step 2.
Example: the number 101 is a palindrome. The number 561, however,
isn't. We take the number's digits in reverse order and add it to the
previous: 165+561 = 726. This is still not a palindrome, so we repeat
the step: 627+726=1,353. This is still not a solution and we repeat
the procedure once more: 3,531+1,353=4,884. This is a palindrome, so
we stop. It has taken us three steps to produce the palindrome.
The program should display the final palindrome and how many steps it
took to produce it.
Specs : (i.e., what your procedure should and should not do)
You will have to implement the following helper functions:
- ReverseNumber: it will take a given number
and return a new number that is made of the first one with it's
digits in reverse order. It takes two parameters:
- The number.
- The reverse number
- IsPalindrome: it will test a number and
determine if it is a palindrome. It takes three parameters:
- The number to be tested.
- The number with the digits in reverse order.
- A flag that is set to TRUE if it is a palindrome,
else it is set to FALSE
Example runs:
Please enter number to start with: 123
Palindrome: 444
Number of reversals: 1
Please enter number to start with: 169
Palindrome: 1441
Number of reversals: 2
Notes :
- I would have main call only
IsPalindrome, and have the latter call
ReverseNumber, but this is not mandatory (i.e., you
could have main call both procedures).
- careful: the number could easily become too large. If you start
with the number 89, you will have to make 24 reversals and you will
end up with the number 8,813,200,023,188 ! You can buy yourself a few
more steps by using longints, but it is still not
enough. You have to make sure that the number does not exceed the
limit.