Bash program to check if the Number is a Palindrome
Last Updated :
05 Sep, 2022
Given a number num, find whether the given number is palindrome or not using Bash Scripting. Examples:
Input :
666
Output :
Number is palindrome
Input :
45667
Output :
Number is NOT palindrome
Approach To find the given number is palindrome just check if the number is same from beginning and the end. Reverse the number to check if the number reversed is equal to the original number or not, if yes than echo Number is palindrome otherwise echo Number is NOT palindrome .
BASH
BASH
num=545
s=0
rev= ""
temp=$num
while [ $num -gt 0 ]
do
s=$(( $num % 10 ))
num=$(( $num / 10 ))
rev=$( echo ${rev}${s} )
done
if [ $temp - eq $rev ];
then
echo "Number is palindrome"
else
echo "Number is NOT palindrome"
fi
|
Output:
Number is palindrome
Time complexity: O(logn) as while loop would run for logn times
Auxiliary space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...