Bash程序来检查数字是否是回文

给定一个数字num,使用Bash脚本查找给定的数字是否为回文。

null

例如:

Input : 
666
Output :
Number is palindrome

Input :
45667
Output :
Number is NOT palindrome

方法 要找到给定的数字是回文的,只需检查数字的开头和结尾是否相同。反转数字以检查反转的数字是否等于原始数字,如果是,则表示回音数字为回文,否则表示回音数字为非回文。

猛击

num=545
# Storing the remainder
s=0
# Store number in reverse
# order
rev= ""
# Store original number
# in another variable
temp=$num
while [ $num -gt 0 ]
do
# Get Remainder
s=$(( $num % 10 ))
# Get next digit
num=$(( $num / 10 ))
# Store previous number and
# current digit in reverse
rev=$( echo ${rev}${s} )
done
if [ $temp - eq $rev ];
then
echo "Number is palindrome"
else
echo "Number is NOT palindrome"
fi


输出:

Number is palindrome

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享