Monday, 13 March 2017

Unix Program To check whether the given number is polindrome or not

Polindrome means a word or a number which gives the same value when  read from both directions(either from left to right order or right to left order.
for example if we take a number 1221 it gives the same number when read from both directions.

The following is the simple unix program to check the given input is polindrome.
 #!/bin/bash

echo -n "Enter number : "
read n
sd=0
rev=""
on=$n
while [ $n -gt 0 ]
do
    sd=$(( $n % 10 )) # get Remainder
    n=$(( $n / 10 ))  # get next digit
    rev=$( echo ${rev}${sd} )
done
if [ $on -eq $rev ];
then
  echo "Number is palindrome"
else
  echo "Number is NOT palindrome"
fi

No comments:

Post a Comment