Shell script to check an input number is palindrome or not palindrome : Solved with Algorithm

Problem Statement

Write a shell script to check a number palindrome or not

Algorithm

Step 1 : Start
Step 2 : Display “Enter a Number”
Step 3 : read n
Step 4 : temp ← n
Step 5 : Repeat step 6 to 9
While temp> 0
Step 6 : ans ← ans*10
Step 7 : mod ← temp%10
Step 8 : ans ← ans+mod
Step 9 : temp ← temp/10
Step 10: If n = ans then
i. Display “It&’s a Palindrome Number”
Step 11: else
i. Display “It’s not a Palindrome Number”
Step 12: Stop

Source Code

#! /bin/bash
#Website : cs.sounak.in
# Author: Sounak Pal
## Script to check a number palindrome or not
echo "Enter a Number"
read n
temp=$n
while ((temp>0))
   do
      ans=$((ans*10))
      mod=$((temp%10))
      ans=$((ans+mod))
      temp=$((temp/10))
   done
if (($n == $ans))
   then
      echo "It's a Palindrome Number"
   else
   echo "It's not a Palindrome Number"
   fi

Input

Enter a Number
46

Output

It’s not a Palindrome Number

Leave a Comment

Your email address will not be published. Required fields are marked *