Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

Print given number in reverse order

clear
echo "Enter a number"
read n
sd=0
rev=0

while [ $n -gt 0 ]
do
sd=$(( $n % 10 ))
rev=$(( $rev \ 10 + $sd ))
n=$(( $n / 10 ))
done

echo "Reverse number of entered digit is $rev"


From the above code I am unable to get the required output. Instead, this error is displayed.

./Display: line 17: 0 \ 10 + 4 : syntax error: operand expected (error token is "\ 10 + 4 ")
Reverse number of entered digit is 0


I don't know why this error is displayed, please help me to figure it out.
by

1 Answer

Bharatgxwzm
Numbers are text too. Text can be reversed with rev without any arithmetic.
#!/bin/bash
clear
read -p "Enter a number: " num
echo $num | rev

Login / Signup to Answer the Question.