1.
Reverse of a number
echo "Enter a number: "
read number
reverse=0
original=$number
while [ $number -ne 0 ]
do
remainder=`expr $number % 10`
reverse=`expr $reverse \* 10 + $remainder`
number=`expr $number / 10`
done
echo "Reverse of the number is $reverse"
2. Check whether the given number is Palindrome or not
echo "Enter a number: "
read number
reverse=0
original=$number
while [ $number -ne 0 ]
do
remainder=`expr $number % 10`
reverse=`expr $reverse \* 10 + $remainder`
number=`expr $number / 10`
done
if [ $original -eq $reverse ]
then
echo "$original is a palindrome."
else
echo "$original is not a palindrome."
fi
3. Check whether the given number is Armstrong or not
echo "Enter a number: "
read c
x=$c
sum=0
r=0
n=0
while [ $x -gt 0 ]
do
r=`expr $x % 10`
n=`expr $r \* $r \* $r`
sum=`expr $sum + $n`
x=`expr $x / 10`
done
if [ $sum -eq $c ]
then
echo "It is an Armstrong Number."
else
echo "It is not an Armstrong Number."
fi
4.Check whether the given number is Prime or not
echo "Enter a number:"
read number
i=2
while [ $i -lt $number ]
do
if [ `expr $number % $i` -eq 0 ]
then
echo "$number is not a prime number."
exit
fi
i=`expr $i + 1`
done
echo "$number is a prime number."
5. Find the sum of first ‘N’ natural numbers
echo "Enter a number: "
read num
i=1
sum=0
while [ $i -le $num ]
do
sum=`expr $sum + $i`
i=`expr $i + 1`
done
echo "The sum of first $num numbers is: $sum"
6. Find the sum of digits of a number
echo "Enter a number"
read num
sum=0
n=$num
while [ $num -gt 0 ]
do
mod=`expr $num % 10`
sum=`expr $sum + $mod`
num=`expr $num / 10`
done
echo "Sum of digits of $n is $sum"
7. Find the sum of ‘N’ numbers
echo "Enter Size(n)"
read n
i=1
sum=0
echo "Enter Numbers"
while [ $i -le $n ]
do
read num
sum=`expr $sum + $num`
i=`expr $i + 1`
done
echo "Sum = $sum"
8. Find the reverse of a string
echo Enter the string
read s
reverse=`expr $s | rev`
echo "Reverse of the string $s is $reverse"
9.Check whether the given string is Palindrome or not
echo Enter the string
read s
temp=`expr $s`
rvs=`expr $temp | rev`
if [ $s = $rvs ]
then
echo "it is palindrome"
else
echo " it is not a Palindrome"
fi