Shell script to find whether an input number is palindrome or not
Posted in Academic » Shell Math
A palindrome is a word, phrase, number or other sequence of units that has the property of reading the same in either direction (the adjustment of punctuation and spaces between words is generally permitted). Composing literature in palindromes is an example of constrained writing. The word "palindrome" was coined from Greek roots palin ("back") and dromos ("way, direction") by English writer Ben Jonson in the 1600s.
A palindromic number is a number where the digits, with decimal representation usually assumed, are the same read backwards, for example, 58285. They are studied in recreational mathematics where palindromic numbers with special properties are sought. A palindromic prime is a palindromic number that is a prime number.
#!/bin/bash # Shell script to find whether an input number is palindrome or not # ----------------------------------------------- # Copyright (c) 2006 nixCraft <http://cyberciti.biz/fb/> # This script is licensed under GNU GPL version 2.0 or above # ------------------------------------------------------------------------- # This script is part of nixCraft shell script collection (NSSC) # Visit http://bash.cyberciti.biz/ for more information. # ------------------------------------------------------------------------- echo -n "Enter number : " read n # store single digit sd=0 # store number in reverse order rev="" # store original number on=$n while [ $n -gt 0 ] do sd=$(( $n % 10 )) # get Remainder n=$(( $n / 10 )) # get next digit # store previous number and current digit in reverse rev=$( echo ${rev}${sd} ) done if [ $on -eq $rev ]; then echo "Number is palindrome" else echo "Number is NOT palindrome" fi
Download - Email this to a friend - Printable version
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!
Tags: bash shell script, eq, fi, input number, loop method, mathematics, read command, remainder, shell loops, while loop ~ Last updated on: April 12, 2008

