Factorial,Greatest Number,String Palindrome using Shell Programming
Menu driven program for Factorial,Greatest Number,String Palindrome
a) Find factorial of a no.
b) Find greatest of three numbers
c) Find a prime no
d) Find whether a number is palindrome
e) Find whether a string is palindrome
Factorial,Greatest Number,String Palindrome Shell Programming Code
palins()
{
echo “ Enter a String to check for Palindrome “
read str
i=1
y=`echo $str | wc -c`
y=`expr $y - 1`
z=$y
echo “The Length of String $y”
while [ $y -ne 0 ]
do
z=`echo $str | cut -c $i`
w=`echo $str | cut -c $y`
if [ "$z" != "$w" ]
then
echo “ The Given String $str is not Palindrome ”
y=0
else
i=`expr $i + 1`
y=`expr $y - 1`
fi
done
if [ "$z" = "$w" ]
then
echo “The Given String $str is a Palindrome”
fi
}
palinn()
{
echo -n "Enter number : "
read n
sd=0
rev=""
on=$n
while [ $n -gt 0 ]
do
sd=$(( $n % 10 )) # get Remainder
n=$(( $n / 10 )) # get next digit
rev=$( echo ${rev}${sd} )
done
if [ $on -eq $rev ];
then
echo "Number is palindrome"
else
echo "Number is NOT palindrome"
fi
}
great()
{ echo "enter first number"
read a1
echo "enter first number"
read a2
echo "enter first number"
read a3
if [ $a1 -gt $a2 ] && [ $a1 -gt $a3 ]; then
echo "first number $a1 is greatest"
elif [ $a2 -gt $a1 ] && [ $a2 -gt $a3 ]; then
echo "second number $a2 is greatest"
elif [ $a3 -gt $a1 ] && [ $a3 -gt $a2 ]; then
echo "third number $a3 is greatest"
fi
}
fact()
{
echo "enter the number to find the factorial"
read b
c=1
while [ "$b" -gt 0 ]; do
c=$(($c * $b))
b=$(($b-1))
done
echo "factorial is $c "
}
clear
echo "1. factorial "
echo "2. greatest number "
echo "3. palindrome "
echo "4. palindrome string "
echo "5. exit"
echo "6. enter ur choice "
read d
case "$d" in
1) echo " factorial "
fact;;
2) echo "greatest"
great;;
3) echo "palindrome"
palinn ;;
4) palins ;;
5) ;;
* ) echo "enter right choice"
esac


Leave a Reply