Bash program to check if the Number is a Palindrome

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Number is palindrome


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : AnmolAgarwal



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.