How to Generate a Palindromic Number

How to Generate a Palindromic Number thumbnail
Palindromic numbers are everywhere!

A palindromic number is any number that is read the same forward as backward, and can occur in any base system. It is common to use a palindromic number generator in computer programming for various functions or as an exercise for beginning programmers. A simple palindromic number function can be written in any language and keyed for any base, but the procedure is essentially the same.

Instructions

    • 1

      Begin your palindromic number generator by creating a input function that tests for integers with at least one digit. A palindromic number, by definition, is an integer, so be sure to weed out any non-integer numbers before beginning.

    • 2

      Add the inputted integer to its reverse (most programming languages have a function to reverse the integer). For example, if your number is 412, your program should add 412 + 214.

    • 3

      Test for palindromic number generation from your first addition. The function (depending on your programming language) should be a boolean test for equality between the generated number and its reverse.

    • 4

      Output the value of the generated number if the test is true. Reverse the generated number and add it back to itself if the test is false. Repeat this loop until your program generates a true value. (Not all integers will generate a palindromic number.)

    • 5

      Add a little extra to your program by inserting a counting function. This function should count how many times the program added a number and its reverse before outputting a result. Be sure to output the count variable along with the result!

Related Searches:

References

Comments

You May Also Like

Related Ads

Featured