Программа Bash для проверки, является ли число палиндромом
Опубликовано: 16 Февраля, 2022
Учитывая число num, выясните, является ли данное число палиндромом или нет, используя Bash Scripting.
Примеры:
Вход :
666
Выход :
Число палиндром
Вход :
45667
Выход :
Число НЕ является палиндромом
Рекомендуется: сначала попробуйте свой подход в {IDE}, прежде чем переходить к решению.
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
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
Output:
Number is palindrome
Previous
Bash program to check if the Number is a Prime or not
Next
Reverse a String | Shell Programming
Recommended Articles
Page :
Bash program to check if the Number is a Prime or not15, Jan 19
Bash program to find A to the power B02, Jan 18
Leap Year Program with Bash Shell Scripting in Windows25, Aug 20
Write a bash script to print a particular line from a file14, Jun 17
Fibonacci Series in Bash11, Feb 18
Useful and time saving bash commands in Linux12, Dec 17
Average of given numbers in Bash31, Jan 18
Sorting an array in Bash using Bubble sort02, Feb 18
Simple Calculator in Bash25, Feb 18
Bash shell script to find sum of digits12, Mar 18
Programs for printing different patterns in Bash23, Jul 18
Bash shell script to swap two numbers05, Jul 18
Bash Script to get Low Battery Alert in Linux12, Jul 19
Bash shell script to find out the largest value from given command line arguments30, Aug 19
How to Fix Minimal BASH Like Line Editing is Supported GRUB Error In Linux?05, Nov 19
How to Code Your Own Port Scanner Using BASH Script and netcat Tool in Linux?04, Jan 21
Bash Brace Expansion In Linux with Examples22, Jan 21
Setting Date and Time For Each Command in Bash History04, Mar 21
Count all palindrome which is square of a palindrome05, Nov 18